【问题标题】:Xamarin Forms content page make background color transparentXamarin Forms 内容页面使背景颜色透明
【发布时间】:2019-02-09 02:10:30
【问题描述】:

我有一个要求,我需要在模式弹出页面中显示一个列表。我可以显示一个列表,但我不能使背景颜色透明或半透明,以便它下面的页面部分可见。

我正在使用以下方式从我的视图模型中推送页面:

NavigationParameters oNavParams = new NavigationParameters();
oNavParams.Add("Mode", "FeedBack");
oNavParams.Add("selectedItem", txtFeedback);
_navigationService.NavigateAsync("PopupBox", oNavParams);

这是我的 xaml 代码:

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:prism="clr-namespace:Prism.Mvvm;assembly=Prism.Forms"
             xmlns:local="clr-namespace:MyApp"
             prism:ViewModelLocator.AutowireViewModel="True"
             x:Class="MyApp.Views.PopupBox">
    <ContentPage.Content>
        <AbsoluteLayout Padding="0" HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand">
            <ContentView x:Name="popupListView" BackgroundColor="Transparent" Padding="10, 0" IsVisible="false" AbsoluteLayout.LayoutBounds="0, 0, 1, 1" AbsoluteLayout.LayoutFlags="All">
                <StackLayout VerticalOptions="Center" HorizontalOptions="Center">
                    <StackLayout Orientation="Vertical" HeightRequest="200" WidthRequest="300" BackgroundColor="White">
                        <ListView x:Name="sampleList">
                        </ListView>
                    </StackLayout>
                </StackLayout>
            </ContentView>
        </AbsoluteLayout>
    </ContentPage.Content>
</ContentPage>

这是我的代码:

  public PopupBox()
        {
            try
            {
                InitializeComponent();
                NavigationPage.SetHasNavigationBar(this, false);
                sampleList.ItemsSource = new List<string>
            {
                "Test ListView",
                "Test ListView",
                "Test ListView",
                "Test ListView",
                "Test ListView",
                "Test ListView",
                "Test ListView",
                "Test ListView",
                "Test ListView",
                "Test ListView",
                "Test ListView"
            };

                 popupListView.IsVisible = true;


            }
            catch (Exception ex)
            {

            }
            }

这是输出:

我也尝试过设置以下内容:

  this.BackgroundColor= new Color(0, 0, 0, 0.4);

但它不起作用。有什么方法可以实现这一点?使用自定义渲染器或任何其他解决方法来显示模式。 我不想使用 Rg.Plugins.Popup,因为我遇到了问题。所以我正在寻找替代方案。请帮忙。

【问题讨论】:

    标签: xamarin.forms custom-renderer modalpopup


    【解决方案1】:

    如果没有自定义渲染器,这将无法工作。 今天获得所需的常用方法是简单地使用适用于 Xamarin 表单的弹出页面插件 (https://github.com/rotorgames/Rg.Plugins.Popup) nuget Rg.Plugins.Popup 可用。

    【讨论】:

    【解决方案2】:

    根据@nicks 的评论,请更改您的代码,我将添加一些可能对您有所帮助的示例代码行。Rg.Plugins.Popup 使用此插件并删除 ContentPage 添加此插件。

    <?xml version="1.0" encoding="utf-8" ?>
    <pages:PopupPage 
        xmlns="http://xamarin.com/schemas/2014/forms"
        xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
        xmlns:pages="clr-namespace:Rg.Plugins.Popup.Pages;assembly=Rg.Plugins.Popup"
        xmlns:animations="clr-namespace:Rg.Plugins.Popup.Animations;assembly=Rg.Plugins.Popup">
    
               <ListView x:Name="lst" BackgroundColor="Gray"  HasUnevenRows="True" >
                    <ListView.ItemTemplate>
                        <DataTemplate>
                            <ViewCell>
                                <Grid HorizontalOptions="FillAndExpand" Padding="10" BackgroundColor="White">
                                    <Grid.RowDefinitions>
                                        <RowDefinition Height="Auto"/>
                                        <RowDefinition Height="Auto"/>
                                        <RowDefinition Height="Auto"/>
                                    </Grid.RowDefinitions>
                                    <Grid.ColumnDefinitions>
                                        <ColumnDefinition Width="Auto"/>
                                        <ColumnDefinition Width="Auto"/>
                                    </Grid.ColumnDefinitions>
                                    <Label Text="{Binding DisplayName}" HorizontalOptions="StartAndExpand" Grid.Row="0"  Grid.Column="0" FontAttributes="Bold"/>
                                    <Label Text="{Binding DisplayContact}" HorizontalOptions="EndAndExpand" Grid.Row="0" Grid.Column="1" FontSize="11"/>
                                    <Label Text="{Binding DisplayAddress}" VerticalOptions="StartAndExpand" Grid.Row="1" Grid.Column="0" FontSize="11"/>
                                </Grid>
                            </ViewCell>
                        </DataTemplate>
                    </ListView.ItemTemplate>
                </ListView>
    </pages:PopupPage>
    

    .cs 文件

    using Rg.Plugins.Popup.Extensions;
    using Rg.Plugins.Popup.Pages;
    
    [XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class className : PopupPage
    {
    
    }
    

    所以在最后一次调用上面的类作为按钮点击等弹出窗口之后,下面是代码

    using Rg.Plugins.Popup.Extensions;
    {
    ClassName _className = new ClassName();
    void Button1Click(object sender, System.EventArgs e)
      {
        Navigation.PushPopupAsync(_className);
       }
    }
    

    希望对你有帮助!!!!

    【讨论】:

    • 你能澄清你的答案吗?我认为他正在尝试使内容页面的背景透明,以便 ListView 看起来像 Popup 一样!
    • @NirmalSubedi 由于睡眠不足,我发布了错误的帖子,很抱歉给您带来不便。现在正确答案已发布。
    【解决方案3】:

    如前所述,您可以使用 Rg.Plugins.Popup,然后将背景设置为与图像中一样透明(使其不仅仅是不透明)。

    我的弹出页面示例:

    点击后:

    Device.BeginInvokeOnMainThread(async () =>
            {
              Task<yourPopUpPage> task = Task.Run(() =>
              {
                return new yourPopUpPage();
              });
    
              task.Wait();
    
              if (task.Status == TaskStatus.RanToCompletion)
              {
                Device.BeginInvokeOnMainThread(async () =>
                {
                  await App.GetCurrentPage().Navigation.PushPopupAsync(task.Result);
                });
              };
            });
    

    【讨论】:

      【解决方案4】:

      如果您更新到 Xamarin Forms 4.6,您可以设置:

      <?xml version="1.0" encoding="utf-8" ?>
      <ContentPage
      ...
      BackgroundColor="Transparent"
      xmlns:ios="clr-namespace:Xamarin.Forms.PlatformConfiguration.iOSSpecific;assembly=Xamarin.Forms.Core"
      ios:Page.ModalPresentationStyle="OverFullScreen">
      
      <StackLayout HorizontalOptions="FillAndExpand"
      
      </StackLayout>
      
      </ContentPage>
      

      它有效。 拉取请求:https://github.com/xamarin/Xamarin.Forms/pull/8551

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-09-21
        • 2012-11-05
        • 2012-06-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多