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,出于某些需要,我必须在我的内容页中添加一个网格。我试过这个: public HomePage() { Grid = new Grid { RowDefinitions = new RowDefinitionCollection() { new RowDefinition() { Height = GridLength.Star } }, ColumnDefinitions = new ColumnDe

出于某些需要,我必须在我的内容页中添加一个网格。我试过这个:

    public HomePage()
    {
        Grid = new Grid
        {
            RowDefinitions = new RowDefinitionCollection() { new RowDefinition() { Height = GridLength.Star } },
            ColumnDefinitions = new ColumnDefinitionCollection() { new ColumnDefinition() { Width = GridLength.Star } },
            VerticalOptions = LayoutOptions.FillAndExpand,
            HorizontalOptions = LayoutOptions.FillAndExpand,
            ColumnSpacing = 0,
            RowSpacing = 0,
        };
    }

    protected override void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        base.OnPropertyChanged(propertyName);
        if (propertyName == ContentProperty.PropertyName)
        {
            if (!IsGridSet)
            {
                Grid.Children.Add(Content);
                IsGridSet = true;
                Content = Grid;
            }
        }
    }

它似乎可以正常工作,因为页面显示正确,但没有一个控件可以正常工作,按钮、条目所有内容似乎都被停用或无法访问,就像另一个透明组件被放置在前台一样

我终于找到了解决办法:微笑:

    public HomePage()
    {
        Grid = new Grid
        {
            RowDefinitions = new RowDefinitionCollection() { new RowDefinition() { Height = GridLength.Star } },
            ColumnDefinitions = new ColumnDefinitionCollection() { new ColumnDefinition() { Width = GridLength.Star } },
            VerticalOptions = LayoutOptions.FillAndExpand,
            HorizontalOptions = LayoutOptions.FillAndExpand,
            ColumnSpacing = 0,
            RowSpacing = 0,
        };
    }
public static BindableProperty HomeContentProperty = BindableProperty.Create(nameof(HomeContent), typeof(Xamarin.Forms.View), typeof(HomePage));
public Xamarin.Forms.View HomeContent
{
    get => (Xamarin.Forms.View)GetValue(HomeContentProperty);
    set => SetValue(HomeContentProperty, value);
}

Grid Grid { get; set; }
protected override void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
    base.OnPropertyChanged(propertyName);
    if (HomeContentProperty.PropertyName == propertyName)
    {
        if (HomeContent != null)
        {
            Grid.Children.Add(HomeContent);
            Content = Grid;
        }
    }
}

我可以在任何地方显示我的警报。

我终于找到了一个解决方案:微笑:

    public HomePage()
    {
        Grid = new Grid
        {
            RowDefinitions = new RowDefinitionCollection() { new RowDefinition() { Height = GridLength.Star } },
            ColumnDefinitions = new ColumnDefinitionCollection() { new ColumnDefinition() { Width = GridLength.Star } },
            VerticalOptions = LayoutOptions.FillAndExpand,
            HorizontalOptions = LayoutOptions.FillAndExpand,
            ColumnSpacing = 0,
            RowSpacing = 0,
        };
    }
public static BindableProperty HomeContentProperty = BindableProperty.Create(nameof(HomeContent), typeof(Xamarin.Forms.View), typeof(HomePage));
public Xamarin.Forms.View HomeContent
{
    get => (Xamarin.Forms.View)GetValue(HomeContentProperty);
    set => SetValue(HomeContentProperty, value);
}

Grid Grid { get; set; }
protected override void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
    base.OnPropertyChanged(propertyName);
    if (HomeContentProperty.PropertyName == propertyName)
    {
        if (HomeContent != null)
        {
            Grid.Children.Add(HomeContent);
            Content = Grid;
        }
    }
}

我可以在任何我想要的地方显示我的警报。

我发现了更多。如果我这样做时,页面加载按钮的行动,它的工作。。。所以我不明白为什么它以前不工作。当你把内容放在网格中时,它到底包含什么?@FreakyAli它包含另一个网格。为什么你要在ViewModel中添加UI元素?它不在ViewModel中……我发现了更多的东西。如果我这样做时,页面加载按钮的行动,它的工作。。。所以我不明白为什么它以前不起作用。当你把它放在网格中时,内容到底包含什么?@FreakyAli它包含另一个网格。你为什么要在ViewModel中添加UI元素?它不在ViewModel中。。。。