gpt4 book ai didi

c# - 是否有等同于 "Application.OpenForms"的 Silverlight?

转载 作者:太空宇宙 更新时间:2023-11-03 11:48:07 28 4
gpt4 key购买 nike

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

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

        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”的等价物可以帮助解决我的问题或如果还有其他方法可以完成我的任务。

最佳答案

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

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

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

在用户输入数据的页面上,您然后创建一个实例此类,通过手动创建实例和设置用户提交时的属性(类似于您在代码中所做的)或者使用数据绑定(bind)来发挥你的优势(这是我更喜欢的)。

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

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

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

<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();

我猜这就是您要找的。

尝试进入整个数据绑定(bind)的东西,它会为您节省大量的代码行。并让我们知道这是否回答了您的问题,或者您是否有更多 :-)

关于c# - 是否有等同于 "Application.OpenForms"的 Silverlight?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2859641/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com