1

我正在学习 c#,并且我被要求使用 WPF 进行一个项目,但我不太了解。我们也在使用 MUI。我正在尝试完成一项非常基本的任务。我有两个页面叫做ClientRNG.xamland ServerRNG.xaml。在ClientRNG.xaml我有两个按钮和两个文本字段中,当每个按钮被按下时,会生成一个随机数并出现在一个文本框中。其中ServerRNG只有一个按钮和一个文本字段,具有与上述相同的功能。所以我最终会得到三个不同的随机数,一个 inServerRNG.xaml和两个 in 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这是类背后的代码

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: 'SplitPage1'

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

客户RNG

<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 发布的解决方案,但我仍在苦苦挣扎

4

2 回答 2

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 容器来解析SplitPage1.
  2. 根据您的代码,似乎有第三方创建了这些页面。如果这是真的,它可以传递它正在创建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
         }
    }
    
  3. 创建一个类,该类将公开一个静态属性和在该属性更改时将触发的事件:

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

在中注册处理程序SplitPage1

public SplitPage1()
    {
        InitializeComponent();
        SharingManager.ValueChanged += UpdateTextBox;

    }

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

Button_Click_1on 中ServerNRG,更新值以触发事件

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

希望这可以帮助

于 2013-10-20T07:16:55.330 回答
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 事件聚合器)在不同窗口之间传递值/命令。

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

于 2013-10-20T08:38:18.527 回答