Is there a Silverlight equivalent to "Application.OpenForms"?

Basically, I'm trying to take information entered by the user on one page and print it on another page using a "printable" version or report. I have a MainPage.xaml which, as the name suggests, is on my main page, but the window has a sub-page AdCalculator.xaml where the user enters information and a PrintEstimate.xaml which is navigated by a button on the AdCalculator.

I would like to be able to pass information entered into text boxes from AdCalculator and print it through text blocks in PrintEstimate. So for this I have the following code:

        Views.AdCalculator AdCalc = new Views.AdCalculator();
        string PrintCompanyName = AdCalc.CompanyName;
        string PrintContactName = AdCalc.txt_CustomerName.Text;
        string PrintBillingAddress1 = AdCalc.txt_BillingAddress.Text;
        string PrintBillingAddress2 = AdCalc.txt_BillingAddressLine2.Text;
        string PrintPhoneNumber = AdCalc.txt_PhoneNumber.Text;
        string PrintNumOfAds = AdCalc.txt_NumofAds.Text;
        string PrintRateOfPlay = AdCalc.Cmb_Rate.SelectedValue.ToString();
        string PrintNumOfMonths = AdCalc.txt_NumofMonths.Text;
        string PrintTotalDue = AdCalc.txt_InvoiceSummary_TotalDue.Text;

        PrintEstimate PrintEstimatePage = new PrintEstimate();
        PrintEstimatePage.txt_CompanyName.Text = PrintCompanyName;
        PrintEstimatePage.txt_CustomerName.Text = PrintContactName;
        PrintEstimatePage.txt_BillingAddress.Text = PrintBillingAddress1;
        PrintEstimatePage.txt_BillingAddressLine2.Text = PrintBillingAddress2;
        PrintEstimatePage.txt_PhoneNumber.Text = PrintPhoneNumber;
        PrintEstimatePage.txt_InvoiceSummary_NumofAds.Text = PrintNumOfAds;
        PrintEstimatePage.txt_InvoiceSummary_RateofPlay.Text = PrintRateOfPlay;
        PrintEstimatePage.txt_InvoiceSummary_NumOfMonths.Text = PrintNumOfMonths;
        PrintEstimatePage.txt_EstimateTotal.Text = PrintTotalDue;

      

The only problem is that when I instantiate a new AdCalculator page, it clears the values, so nothing is actually saved until user input. Following a colleague's example, I believe that all I have to do is change the line

        Views.AdCalculator AdCalc = new Views.AdCalculator();

      

to

        Views.AdCalculator AdCalc = (AdCalculator)Application.OpenForms["AdCalculator"]; 

      

other than "Apllication.OpenForms" is not registered. I know there are many differences in how the C # code is laid out for Silverlight applications, so I didn't know if there was an equivalent that anyone knew about "Application.OpenForms" that would help solve my problem or if there was some other way to accomplish my task.

+2


a source to share


1 answer


If I understand your question correctly, you just want to get some user input and display it.

I suggest that you start by defining a class that will represent the data you enter, for example:

public class Customer
{
    public string ContectName { get; set; }
    public string BillingAddress1 { get; set; }
    public string BillingAddress2 { get; set; }
    public string PhoneNumber { get; set; }
    public int NumOfAds { get; set; }
    public double RateOfPlay { get; set; }
    public int NumOfMonths { get; set; }
    public double TotalDue { get; set; }
}

      

On the page where the user enters data, you create an instance of this class by manually instantiating and setting its properties when the user submits (similar to what you do in your code) or use data binding to your advantage (which I prefer).

Let's say for example you are entering data into your MainPage

void MainPage_Loaded(object sender, RoutedEventArgs e)
{
    this.DataContext = new Customer();
}

      

Now you can bind the controls. Let's say you are using a grid:

<Grid x:Name="LayoutRoot" Background="White">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto" />
            <ColumnDefinition Width="Auto" />
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
        </Grid.RowDefinitions>
        <sdk:Label Content="Billing Address 1:" Grid.Column="0" Grid.Row="0" HorizontalAlignment="Left" Margin="3" VerticalAlignment="Center" />
        <TextBox Grid.Column="1" Grid.Row="0" Height="23" HorizontalAlignment="Left" Margin="3" Name="billingAddress1TextBox" Text="{Binding Path=BillingAddress1, Mode=TwoWay, ValidatesOnExceptions=true, NotifyOnValidationError=true}" VerticalAlignment="Center" Width="120" />
        <sdk:Label Content="Billing Address 2:" Grid.Column="0" Grid.Row="1" HorizontalAlignment="Left" Margin="3" VerticalAlignment="Center" />
        <TextBox Grid.Column="1" Grid.Row="1" Height="23" HorizontalAlignment="Left" Margin="3" Name="billingAddress2TextBox" Text="{Binding Path=BillingAddress2, Mode=TwoWay, ValidatesOnExceptions=true, NotifyOnValidationError=true}" VerticalAlignment="Center" Width="120" />
        <sdk:Label Content="Contect Name:" Grid.Column="0" Grid.Row="2" HorizontalAlignment="Left" Margin="3" VerticalAlignment="Center" />
        <TextBox Grid.Column="1" Grid.Row="2" Height="23" HorizontalAlignment="Left" Margin="3" Name="contectNameTextBox" Text="{Binding Path=ContectName, Mode=TwoWay, ValidatesOnExceptions=true, NotifyOnValidationError=true}" VerticalAlignment="Center" Width="120" />
        <sdk:Label Content="Num Of Ads:" Grid.Column="0" Grid.Row="3" HorizontalAlignment="Left" Margin="3" VerticalAlignment="Center" />
        <TextBox Grid.Column="1" Grid.Row="3" Height="23" HorizontalAlignment="Left" Margin="3" Name="numOfAdsTextBox" Text="{Binding Path=NumOfAds, Mode=TwoWay, ValidatesOnExceptions=true, NotifyOnValidationError=true}" VerticalAlignment="Center" Width="120" />
        <sdk:Label Content="Num Of Months:" Grid.Column="0" Grid.Row="4" HorizontalAlignment="Left" Margin="3" VerticalAlignment="Center" />
        <TextBox Grid.Column="1" Grid.Row="4" Height="23" HorizontalAlignment="Left" Margin="3" Name="numOfMonthsTextBox" Text="{Binding Path=NumOfMonths, Mode=TwoWay, ValidatesOnExceptions=true, NotifyOnValidationError=true}" VerticalAlignment="Center" Width="120" />
        <sdk:Label Content="Phone Number:" Grid.Column="0" Grid.Row="5" HorizontalAlignment="Left" Margin="3" VerticalAlignment="Center" />
        <TextBox Grid.Column="1" Grid.Row="5" Height="23" HorizontalAlignment="Left" Margin="3" Name="phoneNumberTextBox" Text="{Binding Path=PhoneNumber, Mode=TwoWay, ValidatesOnExceptions=true, NotifyOnValidationError=true}" VerticalAlignment="Center" Width="120" />
        <sdk:Label Content="Rate Of Play:" Grid.Column="0" Grid.Row="6" HorizontalAlignment="Left" Margin="3" VerticalAlignment="Center" />
        <TextBox Grid.Column="1" Grid.Row="6" Height="23" HorizontalAlignment="Left" Margin="3" Name="rateOfPlayTextBox" Text="{Binding Path=RateOfPlay, Mode=TwoWay, ValidatesOnExceptions=true, NotifyOnValidationError=true}" VerticalAlignment="Center" Width="120" />
        <sdk:Label Content="Total Due:" Grid.Column="0" Grid.Row="7" HorizontalAlignment="Left" Margin="3" VerticalAlignment="Center" />
        <TextBox Grid.Column="1" Grid.Row="7" Height="23" HorizontalAlignment="Left" Margin="3" Name="totalDueTextBox" Text="{Binding Path=TotalDue, Mode=TwoWay, ValidatesOnExceptions=true, NotifyOnValidationError=true}" VerticalAlignment="Center" Width="120" />
    </Grid>
</Grid>

      



When the user clicks on the submit button, you can use something like this:

private void Button_Click(object sender, RoutedEventArgs e)
{
    var currentCustomer = this.DataContext as Customer;
    var previewWindow = new PrintPreviewWindow(currentCustomer);
    previewWindow.Show();
}

      

To do this, you need a Silverlight ChildWindow:

public partial class PrintPreviewWindow : ChildWindow
{
    public PrintPreviewWindow(Customer customer)
    {
        InitializeComponent();
        this.DataContext = customer;
    }

    private void OKButton_Click(object sender, RoutedEventArgs e)
    {
        this.DialogResult = true;
    }

    private void CancelButton_Click(object sender, RoutedEventArgs e)
    {
        this.DialogResult = false;
    }
}

      

So your MainPage creates a new PrintPreviewChildWindow instance (it can also be a page if you prefer) and traverses the client instance. Then ChildWindow can do whatever it wants. When ChildWindow closes you probably want to clear the input page, you can do this simply by changing the data context:

this.DataContext = new Customer();

      

I guess this is what you are looking for.

Try to penetrate all the data related to data binding, it will save you many, many lines of code. And let us know if this answers your question or if you have more :-)

+2


a source







All Articles