c# - 如何从后面的代码访问xaml中声明的全局变量

标签 c# wpf xaml resources

我想从 wpf 中的不同导航页面访问一个对象。为此,我创建了一个类并在 app.xaml 中声明。我可以从 xaml 中的多个导航页面访问该类,但是当我想在后面的代码中创建按钮单击事件时,我无法访问该类。

这就是我所做的。

类(SerialComm.cs)。

class SerialComm : INotifyPropertyChanged
{
    private SerialPort _serialPortComm;

    public SerialComm()
    {
        _serialPortComm = new SerialPort();
    }

    public SerialPort SerialPortComm
    {
        get { return _serialPortComm; }
        set
        {
            _serialPortComm = value;
            OnPropertyChanged("SerialPortComm");
        }
    }

    #region NotifyPropertyChange handler
    public event PropertyChangedEventHandler PropertyChanged;

    [NotifyPropertyChangedInvocator]
    protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
    }
    #endregion

}

资源字典(/Resources/DataSourceResources.xaml)

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                xmlns:sys="clr-namespace:System;assembly=mscorlib" 
                xmlns:local="clr-namespace:RemoteConfigurator"
                xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
                xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
                mc:Ignorable="d" 
                >
<local:SerialComm x:Key="SerialCommDataSource" d:IsDataSource="True" />

app.xaml 中的声明

<Application x:Class="RemoteConfigurator.App"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:local="clr-namespace:RemoteConfigurator"
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         mc:Ignorable="d" 
         StartupUri="MainWindow.xaml">
<Application.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="/Resources/DataSourceResources.xaml"/>
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Application.Resources>

导航页面,我可以在其中访问该对象。

<UserControl x:Class="RemoteConfigurator.Content.SerialSettings"
         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:ports="clr-namespace:System.IO.Ports;assembly=System"
         xmlns:sys="clr-namespace:System;assembly=mscorlib" 
         xmlns:local="clr-namespace:RemoteConfigurator"
         mc:Ignorable="d" 
         d:DesignHeight="600" d:DesignWidth="600" Loaded="SerialSettings_Loaded">

<Grid Style="{StaticResource ContentRoot}" DataContext="{Binding Source={StaticResource SerialCommDataSource}}" >
    <ScrollViewer>
        <StackPanel >
            <StackPanel Orientation="Horizontal" Margin="4">
                <TextBlock TextWrapping="Wrap" Text="Baud Rate (bps)" VerticalAlignment="Center" MinWidth="150"/>
                <TextBox x:Name="tbbaudRate" Height="23" TextWrapping="Wrap" MinWidth="200" Text="{Binding SerialPortComm.BaudRate}" />
            </StackPanel>
        </StackPanel>
    </ScrollViewer>
    **<Button Content="Connect" HorizontalAlignment="Right" VerticalAlignment="Bottom" Click="Button_Connect"/>**
</Grid>

对我来说,问题是如何从代码后面访问串行端口?该类实际在哪里声明的。 iirc,我从不调用任何串行端口构造函数。

这是背后的代码。

        private void Button_Connect(object sender, RoutedEventArgs e)
    {
        **//SerialPortComm - doesnt work**
        **//SerialCommDataSource - doest work**
    }

如何从代码隐藏中访问串行端口对象?

谢谢。

最佳答案

你可以做到

App.Current.Resources["SerialCommDataSource"] as SerialCom;

基本上,当您使用 SerialCommDataSource 键添加全局资源时,您可以像上面一样获取它

关于c# - 如何从后面的代码访问xaml中声明的全局变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15747465/

相关文章:

c# - 如何在 DataTrigger 中使用预定义样式

wpf - 将文本 block 绑定(bind)到纯 xaml 中的当前列表框项目

c# - 在网格中使用 2 个不同的 ViewModel

c# - 使用参数覆盖包装 ioc resolve

C#,以编程方式查找任何过去的 BSOD 并获取代码

c# - 类似 Windows 8 开始菜单的 WPF Tile 控件

c# - ReportViewer 控件在 Toolbox visual studio 2015 中不可见

c# - 既然可以继承普通类,为什么还需要抽象类?

c# - 命令执行期间遇到 fatal error

c# - 将 View (xaml) 中的 MedialElement 绑定(bind)到 ViewModel 的属性