Xamarin.forms Xamarin表单内容页使背景色透明

Xamarin.forms Xamarin表单内容页使背景色透明,xamarin.forms,custom-renderer,modalpopup,Xamarin.forms,Custom Renderer,Modalpopup,我有一个要求,我需要在模式弹出页面中显示列表。我可以显示列表,但我不能使背景颜色透明或半透明,以便其下的页面部分可见 我正在使用以下命令从视图模型中推送页面: NavigationParameters oNavParams = new NavigationParameters(); oNavParams.Add("Mode", "FeedBack"); oNavParams.Add("selectedItem", txtFeedback); _navigationService.Navigate

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

我正在使用以下命令从视图模型中推送页面:

NavigationParameters oNavParams = new NavigationParameters();
oNavParams.Add("Mode", "FeedBack");
oNavParams.Add("selectedItem", txtFeedback);
_navigationService.NavigateAsync("PopupBox", oNavParams);
<?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)
            {

            }
            }
这是我的xaml代码:

NavigationParameters oNavParams = new NavigationParameters();
oNavParams.Add("Mode", "FeedBack");
oNavParams.Add("selectedItem", txtFeedback);
_navigationService.NavigateAsync("PopupBox", oNavParams);
<?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)
            {

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

如果没有自定义渲染器,这将无法工作。
现在获得所需的常用方法是简单地使用Xamarin Forms()的弹出页面插件nuget
Rg.Plugins.Popup
可用。

如果没有自定义渲染器,这将无法工作。
现在获得所需内容的一种常见方法是简单地使用Xamarin Forms()的弹出页面插件nuget
Rg.Plugins.Popup
可用。

根据@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>
所以在最后一次调用上面的类作为按钮点击的弹出窗口后,下面是代码

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

希望这能帮助你

根据@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>
所以在最后一次调用上面的类作为按钮点击的弹出窗口后,下面是代码

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

希望这能帮助你

如前所述,您可以使用Rg.Plugins.Popup,然后只需将背景设置为图像中的透明(这样它就不仅仅是不透明的)

我的弹出页面示例:

NavigationParameters oNavParams = new NavigationParameters();
oNavParams.Add("Mode", "FeedBack");
oNavParams.Add("selectedItem", txtFeedback);
_navigationService.NavigateAsync("PopupBox", oNavParams);
<?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)
            {

            }
            }

点击:

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);
            });
          };
        });
Device.beginInvokeMainThread(异步()=>
{
任务=任务。运行(()=>
{
返回新的yourPopUpPage();
});
task.Wait();
if(task.Status==TaskStatus.RanToCompletion)
{
Device.beginInvokeMainThread(异步()=>
{
等待App.GetCurrentPage().Navigation.PushPopupAsync(task.Result);
});
};
});

如前所述,您可以使用Rg.Plugins.Popup,然后只需将背景设置为图像中的透明(这样它就不仅仅是不透明的)

我的弹出页面示例:

NavigationParameters oNavParams = new NavigationParameters();
oNavParams.Add("Mode", "FeedBack");
oNavParams.Add("selectedItem", txtFeedback);
_navigationService.NavigateAsync("PopupBox", oNavParams);
<?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)
            {

            }
            }

点击:

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);
            });
          };
        });
Device.beginInvokeMainThread(异步()=>
{
任务=任务。运行(()=>
{
返回新的yourPopUpPage();
});
task.Wait();
if(task.Status==TaskStatus.RanToCompletion)
{
Device.beginInvokeMainThread(异步()=>
{
等待App.GetCurrentPage().Navigation.PushPopupAsync(task.Result);
});
};
});

如果您更新到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>


如果您更新到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>


我对Rg.Plugins.Popup有问题。我已经请求了帮助()。你能建议我如何使用自定义渲染器吗?谢谢你的回复。我会尝试一下。我在这里描述了一种如何创建自定义弹出窗口(包括透明背景)的方法:建议
从现有库复制
渲染器,最可能不是最好的建议。我让Rg.Popup.Plugin工作了。检查这里的链接()我对Rg.Plugins.Popup有问题。我已经请求了帮助()。你能建议我如何使用自定义渲染器吗?谢谢你的回复。我会尝试一下。我已经描述了如何创建自定义弹出窗口的方法(包括透明背景)这里:建议从现有库中复制渲染器,很可能不是最好的建议。我得到了Rg.Popup.Plugin。请检查此处的链接()你能澄清你的答案吗?我想他是想让内容页的背景变得透明,这样列表视图看起来就像是弹出窗口一样!@NirmalSubedi由于睡眠不足,我发布了错误的帖子。很抱歉给你带来不便。现在发布了正确的答案。你能澄清你的答案吗?我想他是想让内容页的背景变得透明nd是透明的,因此ListView看起来像它的弹出窗口!@NirmalSubedi由于睡眠不足,我发布了错误的帖子。很抱歉给您带来不便。现在发布了正确的答案。