Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/294.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何通过方法绑定标签的可见性_C#_Mvvm_Xamarin.forms - Fatal编程技术网

C# 如何通过方法绑定标签的可见性

C# 如何通过方法绑定标签的可见性,c#,mvvm,xamarin.forms,C#,Mvvm,Xamarin.forms,我有几个标签。我需要多次将visiblity设置为true或false。如果这样做,我的代码将重复如何使用mvvm克服此问题 userlabel.IsVisible = false; passwordlabel.IsVisible = false; userlabel1.IsVisible = true; passwordlabel1.IsVisible = true;

我有几个标签。我需要多次将visiblity设置为true或false。如果这样做,我的代码将重复如何使用mvvm克服此问题

                userlabel.IsVisible = false;
                passwordlabel.IsVisible = false;
                userlabel1.IsVisible = true;
                passwordlabel1.IsVisible = true;

在我的代码中,这段代码多次重截线。我需要把这个放到我的视图模型cs中的方法中,我想您可以在ViewModel类中设置属性的值,并将它们绑定到视图中。在这个示例中,为了清晰起见,我使用了一个基本的ViewModel,它将引发NotifyPropertyChanged事件

 public class MainViewModel : ViewModelBase
    {

        private bool _isUserLabelVisible = false;

        public bool IsUserLabelVisible
        {
            get
            {
                return _isUserLabelVisible;
            }

            set
            {
                Set(ref _isUserLabelVisible, value);
            }
        }


        private bool _isPasswordVisible = true;

        public bool IsPasswordLabelVisible
        {
            get
            {
                return _isPasswordVisible;
            }

            set
            {
                Set(ref _isPasswordVisible, value);
            }
        }

        private void DisableAll()
        {
            IsPasswordLabelVisible = false;
            IsUserLabelVisible = false;
        }

        private void EnableAll()
        {
            IsUserLabelVisible = true;
            IsPasswordLabelVisible = true;
        }
    }
通过这种方式,您可以创建您的方法,并且视图将被更新。 在您看来,您需要使用BooleanToVisibilityConverter

<Window x:Class="StackOverflow54741515.MainWindow"
        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:ignore="http://www.galasoft.ch/ignore"
        mc:Ignorable="d ignore"
        Height="300"
        Width="300"
        Title="MVVM Light Application"
        DataContext="{Binding Main, Source={StaticResource Locator}}">

    <Window.Resources>
        <BooleanToVisibilityConverter x:Key="BoolToVis"/>
    </Window.Resources>

    <Grid x:Name="LayoutRoot">
        <TextBlock Name="userLabel" FontSize="36"
                   FontWeight="Bold"
                   Foreground="Purple"
                   Text="Username"
                   VerticalAlignment="Center"
                   HorizontalAlignment="Center"
                   TextWrapping="Wrap"
                   Visibility="{Binding IsUserLabelVisible, Converter={StaticResource BoolToVis}}"/>

        <TextBlock Name="passwordLabel" FontSize="36"
                   FontWeight="Bold"
                   Foreground="Purple"
                   Text="Password"
                   VerticalAlignment="Center"
                   HorizontalAlignment="Center"
                   TextWrapping="Wrap"
                   Visibility="{Binding IsPasswordLabelVisible, Converter={StaticResource BoolToVis}}"/>

    </Grid>
</Window>


希望这对您有所帮助。

您有谁这
userlabel.IsVisible=false如果您正在使用mvvm!