开发者

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 out to another page via a "printer friendly" version, or report, of something. I have a MainPage.xaml that is, as the name suggests, my main page, but in a window there is the subpage AdCalculator.xaml where the user enters the information and PrintEstimate.xaml that is navigated to via a button on AdCalculator.

I would like to be able to transfer the information entered in the textboxes from AdCalculator and print it out via text blocks in PrintEstimate. So in order to do that 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;

Only problem is, when I instantiate the new AdCalculator page, it clears the values, so nothing is actually retained as far as user-input goes. Following a lead from a colleague, I believe all I need to do is change the line

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

to

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

except the "Apllication.OpenForms" doesn't register. I know there are a lot of differences in the way C# code-behind is laid out for silverlight applications, so I didn't know if there was an equivalent that anyone knew about to "Application.OpenForms" that would help solve my issue or if there was any other way to go about getting my task done.


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

I suggest you start by defining a class that will represent the data you are inputting, 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 inputs data you then create an instance of this class, either by manually creating an instance and setting its properties when the user submits (similar to what you are doing in your code) or use databinding to your advantage (which is what I prefer).

Let's say for example you are inputting data on your MainPage

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

Now you can bind the controls. Let's say you're 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 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();
}

For this to work you'll need to have a Silverlight ChildWindow like this:

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 instance of the PrintPreviewChildWindow (could be a page as well if you prefer) and passes along the customer instance. The ChildWindow can then do whatever it wants with it. When the ChildWindow closes, you'll probably want to empty the input page, you can do this by simply setting the data context again:

this.DataContext = new Customer();

I'm guessing this is what you are looking for.

Try to get into the whole data binding stuff, it will save you lots and lots of lines of code. And let us know if this answers your question or if you have more :-)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜