我正在使用C#WPF应用程序,其中MainWindow
XAML定义了一系列菜单选项卡。
我有一个UserControl
类用于许可设置,需要根据特定活动切换这些标签的可见性。
我的第一次尝试是在MainWindow
中创建公共方法,并从UserControl
类调用它。
Window mainWindow = Application.Current.MainWindow;
mainWindow.myPublicMethod();
这不起作用,因为我的方法未在Window
类中定义。
然后我尝试将其转换为MainWindow
对象:
MainWindow mainWindow = (MainWindow)Application.Current.MainWindow;
mainWindow.myPublicMethod();
这不起作用,因为它找不到类型MainWindow
,因为它位于不同的项目中。
我尝试添加对该项目的引用,但它抱怨循环依赖。
是否有推荐的方法来完成我需要做的事情?
UserControl类代码
public event EventHandler licenseActivated;
protected void onLicenseActivated()
{
var handler = licenseActivated;
if (handler != null)
{
handler(this, EventArgs.Empty);
}
}
MainWindow.xaml代码
在结束标记
之前的右下角添加了事件<telerik:RadTabControl Grid.Row="0" Margin="1,1,1,0" Background="White" ItemContainerStyle="{StaticResource ShellTabItemStyleMain}"
x:Name="ContentGrid" HeaderBackground="{StaticResource HeaderBackground1}" SelectionChanged="ContentGrid_SelectionChanged"
BorderThickness="0">
<telerik:RadTabItem Name="ModelCatalogTab" MouseDown="ModelCatalogTab_MouseDown" VerticalAlignment="Center" >
<telerik:RadTabItem.Header>
<StackPanel Background="Transparent" Orientation="Horizontal" Margin="0">
<Image Width="16" Height="16" Margin="0,0,0,4" x:Name="MyModelsImage" Source="{StaticResource MyImgModelsBlue}" />
<TextBlock Margin="3,0,0,4">My Models</TextBlock>
</StackPanel>
</telerik:RadTabItem.Header>
<uc:ucwpfCatalog x:Name="ModelCatalog" Width="Auto" Height="Auto" HorizontalAlignment="Left" Foreground="{StaticResource defaultForeground}"/>
</telerik:RadTabItem>
<telerik:RadTabItem Name="DataSetTab" IsEnabled="False" Foreground="{StaticResource DarkBrush}" MouseDown="DataSetTab_MouseDown" VerticalAlignment="Center">
<telerik:RadTabItem.Header>
<StackPanel Background="Transparent" Orientation="Horizontal" Margin="0">
<Image Width="16" Height="16" Margin="0,0,0,4" x:Name="MyDataSetImage" Source="{StaticResource MyImgDataSetGray}" />
<TextBlock Margin="3,0,0,4">Data</TextBlock>
</StackPanel>
</telerik:RadTabItem.Header>
</telerik:RadTabItem>
<telerik:RadTabItem Name="VariablesTab" IsEnabled="False" Foreground="{StaticResource DarkBrush}" MouseDown="VariablesTab_MouseDown" VerticalAlignment="Center" >
<telerik:RadTabItem.Header>
<StackPanel Background="Transparent" Orientation="Horizontal" Margin="0">
<Image Width="16" Height="16" Margin="0,0,0,4" x:Name="MyVariablesImage" Source="{StaticResource MyImgVariablesGray}" />
<TextBlock Margin="3,0,0,4">Variables</TextBlock>
</StackPanel>
</telerik:RadTabItem.Header>
</telerik:RadTabItem>
<telerik:RadTabItem Name="ModelBuildingTab" IsEnabled="False" Foreground="{StaticResource DarkBrush}" MouseDown="ModelBuildingTab_MouseDown" VerticalAlignment="Center">
<telerik:RadTabItem.Header>
<StackPanel Background="Transparent" Orientation="Horizontal" Margin="0">
<Image Width="16" Height="16" Margin="0,0,0,4" x:Name="MyModelBuildImage" Source="{StaticResource MyImgModelingGray}" />
<TextBlock Margin="3,0,0,4">Predictive Modeling</TextBlock>
</StackPanel>
</telerik:RadTabItem.Header>
<ucmb:ucwpfModelBuilding x:Name="ModelBuilding" Margin="-10,-5,-12,-12" Width="Auto" Height="Auto" Foreground="{StaticResource defaultForeground}"/>
</telerik:RadTabItem>
<telerik:RadTabItem Name="ClusteringTab" IsEnabled="False" Foreground="{StaticResource DarkBrush}" MouseDown="ClusteringTab_MouseDown" VerticalAlignment="Center">
<telerik:RadTabItem.Header>
<StackPanel Background="Transparent" Orientation="Horizontal" Margin="0">
<Image Width="16" Height="16" Margin="0,0,0,4" x:Name="MyClusteringImage" Source="{StaticResource MyImgClustersGray}" />
<TextBlock Margin="3,0,0,4">Clustering</TextBlock>
</StackPanel>
</telerik:RadTabItem.Header>
</telerik:RadTabItem>
<telerik:RadTabItem Name="LogsViewerTab" IsEnabled="False" Foreground="{StaticResource DarkBrush}" MouseDown="LogsViewerTab_MouseDown" VerticalAlignment="Center">
<telerik:RadTabItem.Header>
<StackPanel Background="Transparent" Orientation="Horizontal" Margin="0">
<Image Width="16" Height="16" Margin="0,0,0,4" x:Name="MyLogsImage" Source="{StaticResource MyImgLogsGray}" />
<TextBlock Margin="3,0,0,4">Logs</TextBlock>
</StackPanel>
</telerik:RadTabItem.Header>
</telerik:RadTabItem>
<telerik:RadTabItem Name="AppSettingsTab" MouseDown="AppSettingsTab_MouseDown" VerticalAlignment="Center">
<telerik:RadTabItem.Header>
<StackPanel Background="Transparent" Orientation="Horizontal" Margin="0">
<Image Width="16" Height="16" Margin="0,0,0,4" x:Name="MyOptionsImage" Source="{StaticResource MyImgSettingsBlue}" />
<TextBlock Margin="3,0,0,4">Settings</TextBlock>
</StackPanel>
</telerik:RadTabItem.Header>
</telerik:RadTabItem>
<uc:ucwpfAppOptions licenseActivated="AppOptions_LicenseActivated" />
</telerik:RadTabControl>
MainWindow.xaml.cs
private void AppOptions_LicenseActivated(object sender, EventArgs e)
{
enableMenuTab(ModelCatalogTab);
}
答案 0 :(得分:2)
这是一个很好的事件用例。
使用事件允许任何父级将此用户控件用于其自身目的,而不仅仅是您的特定MainWindow类。您可以在其他应用程序或此应用程序的其他位置使用usercontrol。我们称之为“松耦合”。
UserControl1.xaml.cs
public event EventHandler ThingHappened;
protected void OnThingHappened()
{
var handler = ThingHappened;
if (handler != null)
{
handler(this, EventArgs.Empty);
}
}
public void SomeRandomMethod()
{
DoStuff();
OnThingHappened();
DoOtherStuff();
}
MainWindow.xaml
<myns:UserControl1 ThingHappened="UserControl1_ThingHappened" />
MainWindow.xaml.cs
private void UserControl1_ThingHappened(object sender, EventArgs e)
{
myPublicMethod();
}