Silverlight是否等同于“Application.OpenForms”?

时间:2010-05-18 17:43:12

标签: c# silverlight-4.0

基本上,我正在尝试将用户输入的信息放在一个页面上,然后通过“打印机友好”版本或报告将其打印到另一页。我有一个MainPage.xaml,顾名思义,我的主页,但在一个窗口中有用户输入信息的子页面AdCalculator.xaml和通过AdCalculator上的按钮导航到的PrintEstimate.xaml。

我希望能够从AdCalculator传输文本框中输入的信息,并通过PrintEstimate中的文本块将其打印出来。所以为了做到这一点,我有以下代码:

        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;

唯一的问题是,当我实例化新的AdCalculator页面时,它会清除这些值,因此就用户输入而言,实际上没有任何内容。在同事的带领下,我相信我需要做的就是改变行

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

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

除了“Apllication.OpenForms”没有注册。我知道在为Silverlight应用程序设置C#代码隐藏方式方面存在很多差异,所以我不知道是否有人知道“Application.OpenForms”有助于解决我的问题或如果有任何其他方法可以完成我的任务。

1 个答案:

答案 0 :(得分:2)

如果我正确理解您的问题,您只需要获得一些用户输入并显示它。

我建议你先定义一个代表你输入的数据的类, 例如:

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; }
}

在用户输入数据的页面上,您可以创建一个实例 通过手动创建实例和设置,可以使用此类 用户提交时的属性(类似于您在代码中执行的操作) 或使用数据绑定为您的优势(这是我更喜欢的。)

例如,假设您在MainPage上输入数据

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

现在您可以绑定控件了。假设您正在使用网格:

<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>

当用户点击提交按钮时,您可以使用以下内容:

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

为此,你需要有一个像这样的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;
    }
}

因此,您的MainPage会创建PrintPreviewChildWindow的新实例(如果您愿意,也可以是页面)并传递客户实例。然后,ChildWindow可以随心所欲地做任何事情。当ChildWindow关闭时,您可能想要清空输入页面,只需再次设置数据上下文即可:

this.DataContext = new Customer();

我猜这就是你要找的东西。

尝试进入整个数据绑定的东西,它将为您节省大量的代码行。如果这回答了您的问题,或者您还有更多问题,请告诉我们: - )