Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/xamarin/3.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
如何在xamarin中使用参数隐藏按钮?_Xamarin_Xamarin.forms - Fatal编程技术网

如何在xamarin中使用参数隐藏按钮?

如何在xamarin中使用参数隐藏按钮?,xamarin,xamarin.forms,Xamarin,Xamarin.forms,如何使用绑定隐藏contentview。如何在平台参数中使用绑定?请检查以下屏幕截图以了解问题 在screen shot示例中,如showjoin是visible true。那就好了。但showjoin为false,则默认情况下显示contentview,因为onflatform中的为true 请帮助我解决这个问题a)在Viewmodel中创建一个布尔属性,例如IsContentViewVisible b) 将此属性绑定到contentview IsVisible属性,例如IsVisible=

如何使用绑定隐藏contentview。如何在平台参数中使用绑定?请检查以下屏幕截图以了解问题

在screen shot示例中,如showjoin是visible true。那就好了。但showjoin为false,则默认情况下显示contentview,因为onflatform中的为true

请帮助我解决这个问题

a)在Viewmodel中创建一个布尔属性,例如IsContentViewVisible

b) 将此属性绑定到contentview IsVisible属性,例如IsVisible=“{Binding IsContentViewVisible}”,还应确保Raisepropertychanged事件已就位


c) 根据您的需要,您可以在ViewModel中将IsContentViewVisible属性设置为False/true

问题还不完全清楚。。。但是,如果希望避免XAML OnPlatform重写(因为它不使用绑定),可以在XAML中删除它,只需在ShowJoinButton bool属性中添加正确的逻辑即可

您可以在代码中检查平台(甚至在ViewModel中),如下所示:

if(Device.RuntimePlatform == Device.iOS) ...
如果您仍然希望将所有内容都放在XAML中,当然这也是可能的。 您可以在
OnPlatform
内部使用绑定,需要注意的是类型参数必须是
BindingBase

<OnPlatform x:TypeArguments="BindingBase" iOS="{Binding MyBool}" Android="{Binding MyBool}"/>

下面是在ViewModel类中编写绑定的代码

private const string ShowJoinButtonPropertyName = "ShowJoinButton";
private bool showJoinButton = false;
public bool ShowJoinButton
{
    get { return showJoinButton; }
    set { SetProperty(ref showJoinButton, value, ShowJoinButtonPropertyName); }
}
无论在何处隐藏/显示,都需要使用以下代码

if (Device.RuntimePlatform == Device.Android)
{
    ShowJoinButton = false;
}
else
{
    ShowJoinButton = true;
}

希望有帮助

如果您只想在您的平台是
Android
时隐藏
ContentView
,我建议您在xaml中使用
OnPlatform
。此外,将ContentView的
IsVisible
属性设置两次通常也不起作用

使用上面的xaml

<OnPlatform x:TypeArguments="View">
    <On Platform="iOS">
        <ContentView Margin="20,10,20,20" HeightRequest="40">
            <!-- Rest of ContentView code -->
        </ContentView>
    </On>

    <!-- You must specify an Android view -->
    <On Platform="Android">
        <!-- Use a simple boxview with height and width 0 to create an empty view -->
        <BoxView HeightRequest="0" WidthRequest="0" IsVisible="False"/>
    </On>
</OnPlatform>


当使用iOS时,这只会在页面上显示
内容视图
,而在Android上则不会显示任何内容

您必须更好地澄清您的问题。你想做什么?我想在onplatformI中使用绑定隐藏contentview我想在onplatformI中使用绑定隐藏contentview我想在xamli中的不可见属性中使用alerady我想在xamli中满足两个条件我已经更新了答案。这对您有用吗?如何在IOS中添加多个contentview在
部分中,使用StackLayout、Grid或其他布局,并将您的项目放在其中。