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永遠不要總是有一個很好的小型完整代碼示例來說明您的問題。如果你的問題不是這麼簡單,那肯定會有所幫助 –