2
我想从wpf中的不同导航页面访问对象。为此,我创建了一个类并在app.xaml中声明。我可以从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**
}
如何从后面的代码访问串口对象?
谢谢。
工程。谢谢。看起来像是在浪费我的时间写下很长的问题:) –
@publicENEMY永远不要总是有一个很好的小型完整代码示例来说明您的问题。如果你的问题不是这么简单,那肯定会有所帮助 –