【问题标题】:Replace Content Page from Xamarin forms page从 Xamarin 表单页面替换内容页面
【发布时间】:2019-09-16 15:28:00
【问题描述】:

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

    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;
            }
        }
    }

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

【问题讨论】:

  • 我发现了更多。如果我在页面加载按钮操作时这样做,它就可以工作......所以我不明白为什么它以前不工作。
  • 当您将内容放入 Grid 时,它究竟包含什么?
  • @FreakyAli 它包含另一个网格。
  • 为什么要在 ViewModel 中添加 UI 元素?
  • 它不在 ViewModel ....

标签: xamarin xamarin.forms


【解决方案1】:

我终于得到了解决方案 :smile:

    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;
        }
    }
}

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

【讨论】:

    猜你喜欢
    • 2016-10-03
    • 1970-01-01
    • 1970-01-01
    • 2017-07-23
    • 2012-10-15
    • 2017-08-01
    • 1970-01-01
    • 2014-05-19
    • 1970-01-01
    相关资源
    最近更新 更多