在两页之间传递值wpf

时间:2013-10-20 06:42:05

标签: c# wpf

我正在学习c#,并且我被要求使用WPF开展一个项目,我对此并不了解。我们也在使用MUI。 我正在努力完成一项非常基本的任务。我有两个名为ClientRNG.xamlServerRNG.xaml的网页。在ClientRNG.xaml我有两个按钮和两个文本字段,当按下每个按钮时,会生成一个随机数,它出现在文本框中。在ServerRNG中,只有一个按钮和一个文本字段,具有与上述相同的功能。 因此,我最终得到三个不同的随机数,一个在ServerRNG.xaml,两个在ClientRNG。 我想要做的是将这些随机数传递给另一个名为SSL.xaml的页面。

页面在MainWindow.xml

中创建

<mui:ModernWindow.MenuLinkGroups>
    <mui:LinkGroup DisplayName="network security">
        <mui:LinkGroup.Links>
            <mui:Link DisplayName="Home" Source="/Pages/Home.xaml" />
            <mui:Link DisplayName="RNG" Source="/Pages/ClientRNG.xaml" />
            <mui:Link DisplayName="3DES" Source="/Pages/3des.xaml" />
            <mui:Link DisplayName="RSA" Source="/Pages/RSA.xaml" />
            <mui:Link DisplayName="SHA-1" Source="/Pages/sha1.xaml" />
            <mui:Link DisplayName="PKI Certificates" Source="/Pages/pki.xaml" />
            <mui:Link DisplayName="SSL" Source="/Pages/SSL.xaml" />

        </mui:LinkGroup.Links>
    </mui:LinkGroup>
    <mui:LinkGroup DisplayName="settings" GroupName="settings">
        <mui:LinkGroup.Links>
            <mui:Link DisplayName="software" Source="/Pages/Settings.xaml" />
        </mui:LinkGroup.Links>
    </mui:LinkGroup>
</mui:ModernWindow.MenuLinkGroups>

<mui:ModernWindow.TitleLinks>
    <mui:Link DisplayName="settings" Source="/Pages/Settings.xaml" />
</mui:ModernWindow.TitleLinks>

ClientRNG中的代码:

namespace NetworkSecuritySSLTest.Pages
{
/// <summary>
/// Interaction logic for RNG.xaml
/// </summary>
public partial class ClientRNG : UserControl
{
    public ClientRNG()
    {
        InitializeComponent();
    }

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        Random r = new Random(1);
        int number = r.Next(0, 100);
        r1Out.Text = number.ToString();
        SharingManager.GlobalValue = number;
    }

    private void Button_Click_1(object sender, RoutedEventArgs e)
    {
        Random r = new Random(3);
        int number = r.Next(0, 100);
        pmsOutC.Text = number.ToString();
    }

这是我在ServerRNG中的代码:

namespace NetworkSecuritySSLTest.Pages
{
/// <summary>
/// Interaction logic for RNG.xaml
/// </summary>
public partial class ServerRNG : UserControl
{

    private SplitPage1 sp;



    public ServerRNG()
    {

        InitializeComponent();

    }

    private void Button_Click_1(object sender, RoutedEventArgs e)
    {
        Random r = new Random(2);
        int number = r.Next(0, 100);
        r2Out.Text = number.ToString();
        SharingManager.GlobalValue = number;
    }
}

}

这是SSL class

背后的代码
namespace NetworkSecuritySSLTest.Pages
{
/// <summary>
/// Interaction logic for SplitPage1.xaml
/// </summary>
public partial class SplitPage1 : UserControl
{
    private int r1FromClient;
    public SplitPage1()
    {
        InitializeComponent();
        SharingManager.ValueChanged += UpdateTextBox1;
        SharingManager.ValueChanged += UpdateTextBox2;

    }

    public void UpdateTextBox1(object sender, NumericEventArgs e)
    {
        r1SSLBox.Text = e.Value.ToString(); // Update textBox
    }

    public void UpdateTextBox2(object sender, NumericEventArgs e)
    {
        r2SSLBox.Text = e.Value.ToString(); // Update textBox
    }

}

} 这是xaml:      &#39; SplitPage1&#39;

<UserControl x:Class="NetworkSecuritySSLTest.Pages.SplitPage1"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         xmlns:mui="http://firstfloorsoftware.com/ModernUI"
         mc:Ignorable="d" 
         d:DesignWidth="766.507" Height="535">
<Grid Style="{StaticResource ContentRoot}">
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="*"/>
        <ColumnDefinition Width="6"/>
        <ColumnDefinition Width="*"/>
    </Grid.ColumnDefinitions>

    <ScrollViewer>
        <StackPanel>
            <TextBlock Text="CLIENT" Style="{StaticResource Heading2}" />
            <TextBlock x:Name="hello" Text="Hello Server. This is my Random Number and my Security Capabilities:" FontSize="12" FontStyle="Italic" Margin="0,10,0,0" UseLayoutRounding="False" TextWrapping="Wrap" />
            <TextBlock x:Name="helloCont" Text="" FontSize="12" FontStyle="Italic" Margin="0,0,0,0"  />
            <TextBox x:Name ="r1SSLBox" HorizontalAlignment="Left" Height="57" Margin="10,88,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120" RenderTransformOrigin="0.498,0.404"/>
            <TextBox x:Name ="r2SSLBox" HorizontalAlignment="Left" Height="57" Margin="10,88,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120" RenderTransformOrigin="0.498,0.404"/>
            <TextBlock x:Name="VerifyDC" Text="I need to verify your Digital Certificate:" FontSize="12" FontStyle="Italic" Margin="0,10,0,0"  />
            <TextBlock x:Name="VerifyCont" Text="" FontSize="12" FontStyle="Italic"  />

            <TextBlock x:Name="MSK" Text="My Master Key is:" FontSize="12" FontStyle="Italic" Margin="0,10,0,0"  />
            <TextBlock x:Name="MSKCont" Text="" FontSize="12" FontStyle="Italic"  />

        </StackPanel>
    </ScrollViewer>
    <GridSplitter Grid.Column="1" />
    <ScrollViewer Grid.Column="2 " Margin="{StaticResource SplitRight}">
        <StackPanel>
            <TextBlock Text="SERVER" Style="{StaticResource Heading2}" />
            <TextBlock Text="Content goes here" />
        </StackPanel>
    </ScrollViewer>
    <GridSplitter Grid.ColumnSpan="3" HorizontalAlignment="Left" Margin="0,23,0,0" VerticalAlignment="Top" Width="735"/>
    <Button Content="Man-In-The-Middle-Attack" HorizontalAlignment="Left" VerticalAlignment="Top" Width="209" RenderTransformOrigin="0.055,0.397" Height="40" Margin="255,451,0,0" Grid.ColumnSpan="3" />

</Grid>

ClientRNG

<UserControl x:Class="NetworkSecuritySSLTest.Pages.ClientRNG"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         xmlns:mui="http://firstfloorsoftware.com/ModernUI"
         mc:Ignorable="d" 
         d:DesignWidth="766.507" Height="535">
<Viewbox Stretch="None">
    <Grid Style="{StaticResource ContentRoot}" Height="301" Margin="0" Width="435">
        <Grid.ColumnDefinitions>
            <ColumnDefinition/>
            <ColumnDefinition Width="0*"/>
        </Grid.ColumnDefinitions>

        <!-- TODO: set @SelectedSource -->

        <mui:ModernTab Layout="Tab" Margin="0,52,0,0">
            <mui:ModernTab.Links>
                <!-- TODO: set @Source -->
                <mui:Link DisplayName="Client" />
                <mui:Link DisplayName="Server" Source="/Pages/ServerRNG.xaml"  />
            </mui:ModernTab.Links>
        </mui:ModernTab>

        <Button Content="GENERATE R# 1" HorizontalAlignment="Left" VerticalAlignment="Top" Width="120"  RenderTransformOrigin="0.055,0.397" Height="26" Margin="10,52,0,0" FontSize="11" Click="Button_Click" />
        <TextBox Name ="r1Out" HorizontalAlignment="Left" Height="57" Margin="10,88,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120" RenderTransformOrigin="0.498,0.404"/>

        <Button Content="GENERATE MS" HorizontalAlignment="Left" VerticalAlignment="Top" Width="119"  RenderTransformOrigin="0.055,0.397" Height="26" Margin="306,52,0,0" Click="Button_Click_2" />
        <TextBox Name ="msOutC" HorizontalAlignment="Left" Height="57" Margin="306,88,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="119" RenderTransformOrigin="0.498,0.404"/>

        <Button Content="GENERATE PMS" HorizontalAlignment="Left" VerticalAlignment="Top" Width="133"  RenderTransformOrigin="0.055,0.397" Height="26" Margin="151,52,0,0" Click="Button_Click_1" />
        <TextBox Name ="pmsOutC" HorizontalAlignment="Left" Height="57" Margin="151,88,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="133" RenderTransformOrigin="0.498,0.404"/>


        <Label Content="Random Number Generator" HorizontalAlignment="Left" Height="19" Margin="10,10,0,0" VerticalAlignment="Top" Width="415" FontWeight="Bold"/>


    </Grid>

</Viewbox>

ServerRNG

<UserControl x:Class="NetworkSecuritySSLTest.Pages.ServerRNG"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         xmlns:mui="http://firstfloorsoftware.com/ModernUI"
         mc:Ignorable="d" 
         d:DesignWidth="766.507" Height="535">
<Viewbox Stretch="None">
    <Grid Style="{StaticResource ContentRoot}" Height="301" Margin="0" Width="435">
        <Grid.ColumnDefinitions>
            <ColumnDefinition/>
            <ColumnDefinition Width="0*"/>
        </Grid.ColumnDefinitions>

        <!-- TODO: set @SelectedSource -->

        <mui:ModernTab Layout="Tab" Margin="0,52,0,0">
            <mui:ModernTab.Links>
                <!-- TODO: set @Source -->
                <mui:Link DisplayName="Client" Source="/Pages/ClientRNG.xaml" />
                <mui:Link DisplayName="Server" />
            </mui:ModernTab.Links>
        </mui:ModernTab>

        <Button Name ="r2but" Content="GENERATE R# 2" HorizontalAlignment="Left" VerticalAlignment="Top" Width="120"  RenderTransformOrigin="0.055,0.397" Height="26" Margin="76,52,0,0" FontSize="11" Click="Button_Click_1" />
        <TextBox Name ="r2Out" HorizontalAlignment="Left" Height="57" Margin="76,88,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120" RenderTransformOrigin="0.498,0.404"/>

        <Button Content="GENERATE MS" HorizontalAlignment="Left" VerticalAlignment="Top" Width="119"  RenderTransformOrigin="0.055,0.397" Height="26" Margin="249,52,0,0" />
        <TextBox Name ="msOutS" HorizontalAlignment="Left" Height="57" Margin="249,88,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="119" RenderTransformOrigin="0.498,0.404"/>


        <Label Content="Random Number Generator" HorizontalAlignment="Left" Height="19" Margin="10,10,0,0" VerticalAlignment="Top" Width="415" FontWeight="Bold"/>


    </Grid>

</Viewbox>

现在我试图使用Omribitan发布的解决方案,但我仍然在努力

2 个答案:

答案 0 :(得分:1)

根据您在评论部分中所说的SplitPage1已经显示,

您在代码中执行的操作是创建SplitPage1实例并将其传递给您的数据

    SplitPage1 sp = new SplitPage1(); // This is a new page, not the one currently shown in your application
    sp.Setr1SSLBox(number); // it should set the text box in SSL page

因此,如果您想设置当前显示的SplitPage1的文本,您需要获取它的参考。很难说是怎么回事,因为我看不到你的整个代码,但这是我要考虑的:

  1. 使用IoC container解析SplitPage1
  2. 的当前实例
  3. 根据您的代码,似乎有第三方创建这些页面。如果这是真的,它可以传递ServerRNG它正在创建的SplitPage1的引用,稍后您将可以使用它,例如:

    public partial class ServerRNG : UserControl
    {
         private SplitPage1 sp;
    
         public ServerRNG(SplitPage1 splitPage) : this()
         {
              sp = splitPage; // Save a reference to the currently displayed `SplitPage1` page
         }
    
         public ServerRNG()
         {
              InitializeComponent();
         }
    
         private void Button_Click_1(object sender, RoutedEventArgs e)
         {
                Random r = new Random(2);
                int number = r.Next(0, 100);
                r2Out.Text = number.ToString();
                sp.Setr1SSLBox(number); // Set the correct instance's text
         }
    }
    
  4. 创建一个类,该类将公开在该属性更改时将触发的静态属性和事件:

    public class SharingManager
    {
        // Define a global static event to be fired when the value is changing
        public static event EventHandler<NumericEventArgs> ValueChanged;
    
        public static int GlobalValue
        {
            set
            {
                // Fire ValueChanged event
                if (ValueChanged != null)
                    ValueChanged(null, new NumericEventArgs(value));
             }
         }
      }
    
     public class NumericEventArgs : EventArgs
     {
        public NumericEventArgs(int value)
        {
            Value = value;
         }
    
         public int Value { get; set; }
     }
    
  5. SplitPage1

    中注册处理程序
    public SplitPage1()
        {
            InitializeComponent();
            SharingManager.ValueChanged += UpdateTextBox;
    
        }
    
    public void UpdateTextBox(object sender, NumericEventArgs e)
    {
        r1SSLBox.Text = e.Value.ToString(); // Update textBox
    }
    

    Button_Click_1 ServerNRG上,更新值以触发事件

        Random r = new Random(2);
        int number = r.Next(0, 100);
        r2Out.Text = number.ToString();
        SharingManager.GlobalValue = number;
    

    希望这有帮助

答案 1 :(得分:0)

您应该使用MVVM设计模式处理WPF。 没有MVVM框架,它有点烦人 我建议你使用http://caliburnmicro.codeplex.com/

使用本教程开始

http://www.mindscapehq.com/blog/index.php/2012/01/12/caliburn-micro-part-1-getting-started/

它还解释了如何使用中介模式(使用caliburn micros event aggerator)在不同窗口之间传递值/命令。

http://www.mindscapehq.com/blog/index.php/2012/2/1/caliburn-micro-part-4-the-event-aggregator/