MVVM轻导航服务-更改主窗口的标题和大小
本文关键字:窗口 标题 导航 服务 MVVM | 更新日期: 2023-09-27 18:18:34
我正在创建一个新的WPF应用程序(我的第一个WPF应用程序):
- 。Net 4.0 <
- MVVM光/gh>c#
- MahApps地铁
我已经在MVVM Light中使用导航服务实现了导航。为了达到同样的目的,我使用了主窗口和页面。
在我的主窗口。我有一个主机正在改变我当前的视图:
<Controls:MetroWindow x:Class="App.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:Controls="clr-namespace:MahApps.Metro.Controls;assembly=MahApps.Metro"
xmlns:resx="clr-namespace:MaverickDesktop.Resources"
Title="My title"
Height="280"
Width="500">
<Window.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Fonts.xaml" />
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Controls.xaml" />
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Accents/Blue.xaml" />
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Accents/BaseLight.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Window.Resources>
<Frame Source="'Views'LoginView.xaml" NavigationUIVisibility="Hidden" Name="MainFrame"></Frame>
</Controls:MetroWindow>
这是LoginView。xaml页面:
<Page x:Class="App.Views.LoginView"
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:Dialog="clr-namespace:MahApps.Metro.Controls.Dialogs;assembly=MahApps.Metro"
Dialog:DialogParticipation.Register="{Binding}"
mc:Ignorable="d"
xmlns:Controls="clr-namespace:MahApps.Metro.Controls;assembly=MahApps.Metro"
xmlns:resx="clr-namespace:MaverickDesktop.Resources"
Height="500"
Width="700"
xmlns:iconPacks="http://metro.mahapps.com/winfx/xaml/iconpacks"
DataContext="{Binding Main, Source={StaticResource Locator}}"
>
<Grid Margin="0,0,0,10">
<Label Content="User:" HorizontalAlignment="Left" Margin="24,37,0,0" VerticalAlignment="Top" FontSize="24"/>
<Label Content="Password:" HorizontalAlignment="Left" Margin="24,96,0,0" VerticalAlignment="Top" FontSize="24"/>
<TextBox HorizontalAlignment="Left" Height="42" Margin="172,37,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="274" Name="txtUser" FontSize="24" Controls:TextBoxHelper.ClearTextButton="True" Text="{Binding personnel.personnel_key, Mode=TwoWay}"/>
<PasswordBox HorizontalAlignment="Left" Height="42" Margin="172,96,0,0" VerticalAlignment="Top" Width="274" Name="txtPassword" FontSize="24" Controls:TextBoxHelper.ClearTextButton="True" PasswordChar="*" PasswordChanged="txtPassword_PasswordChanged"/>
<Button Content="Ingresar" HorizontalAlignment="Left" IsDefault="True" Margin="172,169,0,-22" VerticalAlignment="Top" Width="274" Height="44" FontSize="24" Name="btnLogin" Style="{StaticResource AccentedSquareButtonStyle}" Command="{Binding btn_login_click}"/>
</Grid>
</Page>
问题:
- 如何根据当前视图更改窗口的标题和大小?这可能吗?
首先,你必须理解绑定的概念,你的标题等在MainWindow中是硬编码的值。
现在我能找到的最简单的方法是有一个模型,例如
public class ViewPayload{
public string Title{get;set;}
//more properties here
}
在你的主窗口上,你想要改变的属性
public class MainWindow : MetroWindow{
public string Title{get;set;}
// more properties here
}
现在有趣的是,你可以创建一个pub子事件,像这样
private readonly IEventAggregator _eventAggregator = new EventAggregator();
_eventAggregator.GetEvent<ViewPayload>().Subscribe(ChangePropertiesFromModel);
然后当你想要发布的时候,只要做,
_eventAggregator.GetEvent<ViewPayload>().Publish(PublisPayloadMethod());
并将窗口内容更改为binding,因此
Title="{binding TitleProperty}"