如何根据 OsCheck 类方法的结果将字段的'Visibility'设置为 'Hidden' 或 'Visible'?



我有一个XAML文件,这里没有显示。如何根据OsCheck类方法的结果将字段的"可见性"设置为"隐藏"或"可见"?基本上,如果windows版本是7,我希望字段可见性为"隐藏"。

namespace My.namespace.is.secret
{
  public partial class MyClass: IValueConverter
  {
   public bool OsCheck(){
    System.OperatingSystem os = System.Environment.OSVersion;
    //Get version information about the os.
     System.Version vs = os.Version;
        if ((os.Platform == PlatformID.Win32NT) &&
            (vs.Major == 6) &&
            (vs.Minor != 0))
        {
             return true; //operatingSystem == "7";
        }
        else return false;
       }
    }
}

您需要实现Convert方法,根据您的"OsCheck() "返回一个可见性对象。":

namespace Mynamespace.issecret
{
  public class MyClass: IValueConverter
  {
       public bool OsCheck()
       {
           System.OperatingSystem os = System.Environment.OSVersion;
           //Get version information about the os.
           System.Version vs = os.Version;
           if ((os.Platform == PlatformID.Win32NT) &&
               (vs.Major == 6) &&
               (vs.Minor != 0))
           {
               return true; //operatingSystem == "7";
           }
           return false;
       }
      public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
      {
          return OsCheck() ? Visibility.Collapsed : Visibility.Visible;
      }
      public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
      {
          throw new NotImplementedException();
      }
  }
}

然后在XAML中,您将像这样使用转换器:

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:issecret="clr-namespace:Mynamespace.issecret"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Grid.Resources>
            <issecret:MyClass x:Key="OSCheck" />
        </Grid.Resources>
        <TextBlock Text="This is not Windows 7" Visibility="{Binding Converter={StaticResource OSCheck}}" />
    </Grid>
</Window>

请注意,您的OSCheck方法也将为windows 8/8.1和Server 2008R2-2012返回true: https://en.wikipedia.org/wiki/Windows_NT

相关内容

最新更新