从不同的.xaml.cs文件访问.xaml项目?

时间:2018-12-05 08:47:32

标签: c# wpf xaml

我在三个文件中都有.xaml项。在一个.xaml中,我可以选择在运行时更改语言。

我是否有机会从其他.xaml中访问项目并更改语言?

MainWindow.xaml.cs中.xaml项的更改语言

public partial class MainWindow : Window
{
    string strLanguage = "";
    Boolean boolInit = true;

    private void Window_Loaded(object sender, RoutedEventArgs e)
    {
        Set_Language();
    }

    private void ddlLanguage_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        Set_Language();
    }

    private void Set_Language()
    {
        if (boolInit == false)
        {
            strLanguage = "SMS_Vrana.Languages." + ((ComboBoxItem)ddlLanguage.SelectedItem).Name.ToString();
            ResourceManager LocRM = new ResourceManager(strLanguage, typeof(MainWindow).Assembly);
            lblNumber.Text = LocRM.GetString("strNumber");
            lblMessage.Text = LocRM.GetString("strMessage");
            btnSend.Content = LocRM.GetString("strSend");
            menuItemFile.Header = LocRM.GetString("strMenuFile");
            menuItemMainMenu.Header = LocRM.GetString("strMenuMainMenu");
            menuItemClose.Header = LocRM.GetString("strMenuClose");
            menuItemCheckConnection.Header = LocRM.GetString("strMenuConnection");

        }
    }

.xaml的Mainwindow.xaml:

<grid>
    <Menu DockPanel.Dock="Top" Margin="0,0,0,434">
        <MenuItem Header="_File" Name="menuItemFile">
            <MenuItem Header="_Main Menu" Click="MenuItem_Click" Name="menuItemMainMenu"/>
            <Separator/>
            <MenuItem Header="Close" Click="MenuItem_Close" Name="menuItemClose"/>
        </MenuItem>
        <MenuItem Header="Monitoring" IsEnabled="False">
            <MenuItem Header="Ping" IsCheckable="True" Checked="menuPingCheck" Unchecked="menuPingUncheck"/>
            <MenuItem Header="GSM" IsCheckable="True" Checked="menuGsmCheck" Unchecked="menuGsmUncheck"/>
        </MenuItem>

        <MenuItem Header="Check Connection" Click="menuItemCheckConnection_Click" Name="menuItemCheckConnection"/>

        <ComboBox x:Name="ddlLanguage" SelectionChanged="ddlLanguage_SelectionChanged" Height="23" Width="80">
            <ComboBoxItem IsEnabled="False" IsSelected="True" Content="Language"/>
            <ComboBoxItem x:Name="English" Content="English"/>
            <ComboBoxItem x:Name="Czech" Content="Cesky"/>
        </ComboBox>

        <MenuItem Header="_Language" x:Name="menuLangTest" IsEnabled="True" Visibility="Hidden">
            <MenuItem Header="Czech" x:Name="menuCzech" Checked="menuItemCzechCheck" Unchecked="menuItemCzechUnche"/>
            <MenuItem Header="English" x:Name="menuEng" Checked="menuItemEngCheck" Unchecked="menuItemEngUnche"/>
        </MenuItem>


    </Menu>


    <Ellipse Fill="#000000" Stroke="#000000" Margin="10,0,0,347" x:Name="ellGSM" HorizontalAlignment="Left" Width="38" Height="38" VerticalAlignment="Bottom" Visibility="Hidden"/>
    <Ellipse Fill="#000000" Stroke="#000000" Margin="204,0,0,347" Name="ellCol" HorizontalAlignment="Left" Width="38" Height="38" VerticalAlignment="Bottom" Visibility="Hidden"/>
    <TextBox x:Name="txtNumber" MaxLength="13" HorizontalAlignment="Left" Height="26" TextWrapping="Wrap" VerticalAlignment="Top" Width="310" Margin="10,153,0,0" PreviewTextInput="txtNumber_PreviewTextInput"/>
    <TextBox x:Name="txtMessage" MaxLength="1300" HorizontalAlignment="Left" Height="100" TextWrapping="Wrap" VerticalAlignment="Top" Width="310" Margin="10,250,0,0"/>
    <Button x:Name="btnSend" Cursor="Hand" Content="Send!" HorizontalAlignment="Left" VerticalAlignment="Top" Width="310" Margin="10,407,0,0" Height="52" FontSize="36" Click="btnSend_Click"/>
    <TextBlock Grid.Column="0" Name="lblNumber" Text="Number" Margin="10,122,213,321"  FontSize="16"/>
    <TextBlock Grid.Row="0" Grid.Column="0" Name="lblMessage" Text="Message" Margin="10,217,213,224" FontSize="16"/>
    <TextBlock HorizontalAlignment="Left" TextWrapping="Wrap" Text="GSM" VerticalAlignment="Top" Margin="53,46,0,0" Name="txtblockGsm" Visibility="Hidden"/>
    <TextBlock HorizontalAlignment="Left" TextWrapping="Wrap" Text="PING" VerticalAlignment="Top" Margin="172,46,0,0" Name="txtblockPing" Visibility="Hidden"/>
    <Image HorizontalAlignment="Left" Name="imgGsm" Height="42" VerticalAlignment="Top" Width="49" Margin="271,40,0,0" Source="C:\Users\lvrabel\source\repos\SMS Vrána\SMS Vrána\Images\GsmGreen.png" Visibility="Hidden"/>
    <Label Content="1300" HorizontalAlignment="Left" Margin="287,345,0,0" VerticalAlignment="Top" Height="25" Name="lblLeftCharacters" />
    <Label Content="" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="280,84,0,0" Name="lblGsmSignal" Width="40"/>
   </grid>
我想在MainWindow.xaml.cs中访问Authentication.xaml的

.xaml以更改该项目的语言。我只是想在整个应用程序中更改语言,而不仅仅是更改一个窗口。

<Grid Margin="0,-17,0,0">

    <Menu DockPanel.Dock="Top" Margin="0,17,0,432">
        <MenuItem Header="File" FontSize="16" Name="menuItemFile">
            <MenuItem Header="Back on Main Menu" Click="menuItemMainMenuClick"/>
            <Separator/>
            <MenuItem Header="Close" Click="menuItemCloseClick"/>
        </MenuItem>
    </Menu>



    <Label Content="IP Adress" Name="lblIPAddress" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="10,210,0,0" Height="36" Width="101" FontSize="16"/>
    <Label Content="Login" Name="lblLogin" HorizontalAlignment="Left" Margin="10,279,0,0" VerticalAlignment="Top" Height="31" Width="59" FontSize="16"/>
    <Label Content="Password" Name="lblPassword" HorizontalAlignment="Left" Margin="10,343,0,0" VerticalAlignment="Top" Height="31" Width="75" FontSize="16"/>
    <TextBox HorizontalAlignment="Left" Height="23" TextWrapping="Wrap" VerticalAlignment="Top" Width="292" Margin="10,251,0,0" Name="txtBoxIP" PreviewTextInput="txtBoxIP_PreviewTextInput"/>
    <TextBox HorizontalAlignment="Left" Height="23" TextWrapping="Wrap" VerticalAlignment="Top" Width="292" Margin="10,315,0,0" Name="txtBoxLogin"/>
    <PasswordBox HorizontalAlignment="Left" VerticalAlignment="Top" Width="292" Margin="10,379,0,0" PasswordChar="*" Name="pswBox" Height="23" />
    <CheckBox Content="Remember me!" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="10,413,0,0" Name="CheckBoxRemCredts" Checked="CheckBoxRemCredts_Checked"/>
    <Button Content="Login" Cursor="Hand" HorizontalAlignment="Left" Height="34" Margin="10,442,0,0" VerticalAlignment="Top" Width="292" Name="btnLoginAuth" Click="btnLoginAuth_Click" FontSize="18"/>
</Grid>

2 个答案:

答案 0 :(得分:0)

您所要求的是可能的-当然,您可以在后端文件(.xaml.cs)中获得对某些控件的引用,然后将其分配给两个类都可见的变量。

不过,最好使用事件来完成。您在主类上定义了一个应该对每个窗口可见的事件,然后在每个窗口的代码中订阅该事件。

答案 1 :(得分:0)

  • U可以访问任何xaml的{​​{1}}代码,直到并且除非它们属于相同的项目和解决方案。

  • U只需要初始化xaml文件中要使用的其他xaml。

  • 例如,我有两个xaml文件xaml.csMain.xaml

  • 因此我可以在Login.xaml

  • 中使用登录视图及其元素
  • 语法: Main.xaml.cs

    Main.xaml.cs

  • 现在,只要您为使用的每个元素提供名称,就可以访问“登录”页面的任何控件。

  • 例如:Login login=new Login();

更新后的答案:

嗨,我得到了您要找的东西。您的整个想法是实现“本地化”,与主视图关联的所有视图都将更新。

有多种方法可以实现这一目标,这里我共享一些链接,这些链接可能会对您有所帮助:

https://www.codeproject.com/Articles/22967/WPF-Runtime-Localization

https://www.codeproject.com/Articles/35159/WPF-Localization-Using-RESX-Files

https://social.msdn.microsoft.com/Forums/vstudio/en-US/4a7f049b-f1b7-4982-874f-f9ecfe3d9140/how-to-change-the-language-in-my-view-wpf?forum=wpf

c# - localization - changing language in wpf app

谢谢

如果可以帮助您解决问题,请接受我的回答。