Sometimes we need a hamburger- less page. We do that with the IsFullScreen property. Additional we want to use this IsFullScreen property for switching the visibility of always available common controls.
<UserControl Visibility="{Binding IsVisible, Converter={StaticResource BooleanToVisibilityConverter}}">
</UserControl>
public class MyViewModel : ViewModelBase
{
public bool IsVisible
{
get
{
return !SettingsService.IsFullScreen;
}
}
}
How could we attach the propertychanged event for the IsFullScreen property.
Thanks for your efforts
答案 0 :(得分:1)
您需要在INotifyPropertyChanged
课程中实施SettingsService
。和RaisePropertyChanged();
(这是Binding所必需的。或者你可以使用依赖属性)
像这样:在SettingsService
类
public class SettingsService : INotifyPropertyChanged
{
public static SettingsService Instance { get; } = new SettingsService();
bool _IsFullScreen;
public event EventHandler IsFullScreenChanged;
public bool IsFullScreen
{
get { return _IsFullScreen; }
set
{
if (_IsFullScreen != value)
{
_IsFullScreen = value;
//
RaisePropertyChanged(); //uses CallerMemberName
IsFullScreenChanged?.Invoke(this, EventArgs.Empty);
}
}
}
#region INotifyPropertyChanged
public event PropertyChangedEventHandler PropertyChanged;
public void RaisePropertyChanged([CallerMemberName] string propertyName = null) =>
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
#endregion
}
在MyViewModel
课程中,只需添加属性Settings
。
public class MyViewModel : ViewModelBase
{
public SettingsService Settings => SettingsService.Instance;
//
}
在XAML文件中:在Binding
路径中写入属性路径,如Settings.IsFullScreen
(我使用ValueWhenConverter将bool转换为可见性)
<Page
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:converter="using:Template10.Converters"
mc:Ignorable="d">
<Page.DataContext>
<vm:MyViewModel x:Name="MyViewModel " />
</Page.DataContext>
<Page.Resources>
<converter:ValueWhenConverter x:Key="VisibleWhenTrueConverter">
<converter:ValueWhenConverter.When>
<x:Boolean>True</x:Boolean>
</converter:ValueWhenConverter.When>
<converter:ValueWhenConverter.Value>
<Visibility>Visible</Visibility>
</converter:ValueWhenConverter.Value>
<converter:ValueWhenConverter.Otherwise>
<Visibility>Collapsed</Visibility>
</converter:ValueWhenConverter.Otherwise>
</converter:ValueWhenConverter>
<converter:ValueWhenConverter x:Key="VisibleWhenFalseConverter">
<converter:ValueWhenConverter.When>
<x:Boolean>False</x:Boolean>
</converter:ValueWhenConverter.When>
<converter:ValueWhenConverter.Value>
<Visibility>Visible</Visibility>
</converter:ValueWhenConverter.Value>
<converter:ValueWhenConverter.Otherwise>
<Visibility>Collapsed</Visibility>
</converter:ValueWhenConverter.Otherwise>
</converter:ValueWhenConverter>
</Page.Resources>
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<UserControl Visibility="{Binding Settings.IsFullScreen, Converter={StaticResource VisibleWhenFalseConverter}}">
</UserControl>
</Grid>
答案 1 :(得分:0)
感谢您的回答。我们解决了这个问题如下。我们的SettingsService类是Template10 uwp extension的一部分。对于数据绑定,我们使用MVVM Light工具包。我们广播了一个ScreenSizeChangedArgs
public class ScreenSizeChangedArgs : MessageBase
{
public bool IsFullScreen
{
get; set;
}
public ScreenSizeChangedArgs(bool isFullScreen)
{
IsFullScreen = isFullScreen;
}
}
IsFullScreen属性发生更改时的消息。
public class SettingsService
{
public static SettingsService Instance { get; } = new SettingsService();
public bool IsFullScreen
{
get
{
return m_settingsHelper.Read<bool>(nameof(IsFullScreen), false);
}
set
{
m_settingsHelper.Write(nameof(IsFullScreen), value);
Views.Shell.HamburgerMenu.IsFullScreen = value;
Messenger.Default.Send(new ScreenSizeChangedArgs(value));
}
}
}
在相关的视图模型中,我们注册了ScreenSizeChangedArgs消息并处理新的IsFullScreen值。
public class MyViewModel : ViewModelBase
{
public bool IsVisible
{
get
{
return m_isVisible;
}
set
{
m_isVisible = value;
RaisePropertyChanged("IsVisible");
}
}
public MyViewModel()
{
//The event when the IsFullScreen property has changed.
Messenger.Default.Register<ScreenSizeChangedArgs>(this, args =>
{
IsVisible = !args.IsFullScreen;
});
}
}
对于bool到可见性转换,我们使用类似于http://blogs.u2u.be/diederik/post/2011/11/14/null.aspx的IValueConverter