C# 4.0 有没有一种方法可以在不使用插件的情况下以Xamarin表单(软件模式-mvvm)创建弹出窗口?

C# 4.0 有没有一种方法可以在不使用插件的情况下以Xamarin表单(软件模式-mvvm)创建弹出窗口?,c#-4.0,mvvm,xamarin.forms,plugins,C# 4.0,Mvvm,Xamarin.forms,Plugins,我正在尝试生成弹出窗口,以提醒用户提交或执行其他功能。有人有想法吗? 谢谢,Xamarin.Forms允许您显示警报(带有“确定”按钮的简单消息)、操作表(在不同选项之间选择)和提示(用于文本输入)。这是你的电话号码 如果要创建和显示具有关联viewmodel的视图,则需要使用第三方库。在我看来,这似乎是最受欢迎的选择 Xamarin.Forms允许您显示警报(带有“确定”按钮的简单消息)、操作表(在不同选项之间选择)和提示(用于文本输入)。这是你的电话号码 如果要创建和显示具有关联viewmo

我正在尝试生成弹出窗口,以提醒用户提交或执行其他功能。有人有想法吗?
谢谢,

Xamarin.Forms允许您显示警报(带有“确定”按钮的简单消息)、操作表(在不同选项之间选择)和提示(用于文本输入)。这是你的电话号码


如果要创建和显示具有关联viewmodel的视图,则需要使用第三方库。在我看来,这似乎是最受欢迎的选择

Xamarin.Forms允许您显示警报(带有“确定”按钮的简单消息)、操作表(在不同选项之间选择)和提示(用于文本输入)。这是你的电话号码


如果要创建和显示具有关联viewmodel的视图,则需要使用第三方库。在我看来,这似乎是最受欢迎的选择

对于我的应用程序,我使用App.xaml.cs类,下面是一个示例,如果ContentPage的直接内容是绝对布局,我将使用它在ContentPage上显示ContentView

<?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:d="http://xamarin.com/schemas/2014/forms/design"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             mc:Ignorable="d"
             x:Class="CALogin.Views.LoginPage"
             xmlns:vm="clr-namespace:CALogin.ViewModels">
    <ContentPage.BindingContext>
        <vm:LoginViewModel/>
    </ContentPage.BindingContext>
    <ContentPage.Content>
        <AbsoluteLayout x:Name="MasterLayout" VerticalOptions="FillAndExpand"
                    HorizontalOptions="FillAndExpand" Padding="0">
            <Grid x:Name="MasterGrid" BackgroundColor="Gray">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="*" />
                </Grid.ColumnDefinitions>
                <Grid.RowDefinitions>
                    <RowDefinition Height="*" />
                </Grid.RowDefinitions>
                <!-- Place all of your normal page content here, inside this "MasterGrid" -->
                <!-- When you call ShowPopup(), WaitPopup will be added as the 2nd child of the AbsoluteLayout -->
                <!-- WaitPopup will fill and expand the entire screen, overlapping MasterGrid -->
            </Grid>
        </AbsoluteLayout>
    </ContentPage.Content>
</ContentPage>
App.xaml.cs

        public static void ShowPopup()
        {
            //Get current Page, we assume it's a ContentPage
            var mainPage = App.Current.MainPage as ContentPage;
            //Get the content of that Page, we assume it's an AbsoluteLayout
            var current = mainPage.Content as AbsoluteLayout;
            //Add WaitPopup.cs as a child
            current.Children.Add(new WaitPopup());
            //Re-load the view
            mainPage.Content = current;
        }

        public static void RemovePopup()
        {
            //Get current Page, we assume it's a ContentPage
            var mainPage = Current.MainPage as ContentPage;
            //Get the content of that Page, we assume it's an AbsoluteLayout
            var current = mainPage.Content as AbsoluteLayout;
            //Remove the child from the AbsoluteLayout, in this example it will be WaitPopup
            current.Children.RemoveAt(1);
            //Re-load the view
            mainPage.Content = current;
        }
WaitPopUp.cs,我喜欢使用c#创建弹出窗口,但是如果您愿意,您可以创建一个新的.xaml文件并显示它

public class WaitPopup : ContentView
    {
        public WaitPopup()
        {
            /* We assume we are adding this to an AbsoluteLayout */
            AbsoluteLayout.SetLayoutBounds(this, new Rectangle(0, 0, 1, 1));
            AbsoluteLayout.SetLayoutFlags(this, AbsoluteLayoutFlags.All);
            /*I add opacity so that it looks more like a popup,change background to Transparent if you want this to look more like a true pop up */
            BackgroundColor = Color.White;
            Opacity = 0.77;
            /* All of this content is optional, apply whatever you wish to your PopUp */
            Content = new StackLayout
            {
                /* */
                VerticalOptions = LayoutOptions.Center,
                HorizontalOptions = LayoutOptions.Center,
                /* */
                Children = {
                    /* */
                    new Image {
                        Source = "logo.png",
                        Aspect = Aspect.AspectFill,
                        Margin = new Thickness(100,30,100,20)
                    },
                    /* */
                    new Label {
                        Text = "Espera...",
                        TextColor = Color.Black,
                        HorizontalOptions = LayoutOptions.Center,
                        VerticalOptions = LayoutOptions.Center,
                        VerticalTextAlignment = TextAlignment.Center
                    },
                }
            };
            //Optional, do not use if you do not understand animations
            RotateLogo();
        }
        private void RotateLogo()
        {
            //uint duration = 10 * 60 * 1000;
            var sLayout = this.Content as StackLayout;
            var logo = sLayout.Children[0] as Image;
            logo.RotateYTo(4 * 360, 10000);
        }
    }
Example.xaml文件将与WaitPopup一起使用,请记住,从技术上讲,您可以使此弹出窗口与任何布局兼容,但在本例中,我们假设每个页面都有一个AbsoluteLayout作为其内容,并且当我们添加WaitPopup.cs时,它是AbsoluteLayout的子数组中的第二个子项:

<?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:d="http://xamarin.com/schemas/2014/forms/design"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             mc:Ignorable="d"
             x:Class="CALogin.Views.LoginPage"
             xmlns:vm="clr-namespace:CALogin.ViewModels">
    <ContentPage.BindingContext>
        <vm:LoginViewModel/>
    </ContentPage.BindingContext>
    <ContentPage.Content>
        <AbsoluteLayout x:Name="MasterLayout" VerticalOptions="FillAndExpand"
                    HorizontalOptions="FillAndExpand" Padding="0">
            <Grid x:Name="MasterGrid" BackgroundColor="Gray">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="*" />
                </Grid.ColumnDefinitions>
                <Grid.RowDefinitions>
                    <RowDefinition Height="*" />
                </Grid.RowDefinitions>
                <!-- Place all of your normal page content here, inside this "MasterGrid" -->
                <!-- When you call ShowPopup(), WaitPopup will be added as the 2nd child of the AbsoluteLayout -->
                <!-- WaitPopup will fill and expand the entire screen, overlapping MasterGrid -->
            </Grid>
        </AbsoluteLayout>
    </ContentPage.Content>
</ContentPage>

如果要添加ViewModel,可以在WaitPopup.CS中添加BindingContext


干杯

对于我的应用程序,我使用App.xaml.cs类,下面是一个示例,如果ContentPage的直接内容是绝对布局,我将使用它在ContentPage上显示ContentView

<?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:d="http://xamarin.com/schemas/2014/forms/design"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             mc:Ignorable="d"
             x:Class="CALogin.Views.LoginPage"
             xmlns:vm="clr-namespace:CALogin.ViewModels">
    <ContentPage.BindingContext>
        <vm:LoginViewModel/>
    </ContentPage.BindingContext>
    <ContentPage.Content>
        <AbsoluteLayout x:Name="MasterLayout" VerticalOptions="FillAndExpand"
                    HorizontalOptions="FillAndExpand" Padding="0">
            <Grid x:Name="MasterGrid" BackgroundColor="Gray">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="*" />
                </Grid.ColumnDefinitions>
                <Grid.RowDefinitions>
                    <RowDefinition Height="*" />
                </Grid.RowDefinitions>
                <!-- Place all of your normal page content here, inside this "MasterGrid" -->
                <!-- When you call ShowPopup(), WaitPopup will be added as the 2nd child of the AbsoluteLayout -->
                <!-- WaitPopup will fill and expand the entire screen, overlapping MasterGrid -->
            </Grid>
        </AbsoluteLayout>
    </ContentPage.Content>
</ContentPage>
App.xaml.cs

        public static void ShowPopup()
        {
            //Get current Page, we assume it's a ContentPage
            var mainPage = App.Current.MainPage as ContentPage;
            //Get the content of that Page, we assume it's an AbsoluteLayout
            var current = mainPage.Content as AbsoluteLayout;
            //Add WaitPopup.cs as a child
            current.Children.Add(new WaitPopup());
            //Re-load the view
            mainPage.Content = current;
        }

        public static void RemovePopup()
        {
            //Get current Page, we assume it's a ContentPage
            var mainPage = Current.MainPage as ContentPage;
            //Get the content of that Page, we assume it's an AbsoluteLayout
            var current = mainPage.Content as AbsoluteLayout;
            //Remove the child from the AbsoluteLayout, in this example it will be WaitPopup
            current.Children.RemoveAt(1);
            //Re-load the view
            mainPage.Content = current;
        }
WaitPopUp.cs,我喜欢使用c#创建弹出窗口,但是如果您愿意,您可以创建一个新的.xaml文件并显示它

public class WaitPopup : ContentView
    {
        public WaitPopup()
        {
            /* We assume we are adding this to an AbsoluteLayout */
            AbsoluteLayout.SetLayoutBounds(this, new Rectangle(0, 0, 1, 1));
            AbsoluteLayout.SetLayoutFlags(this, AbsoluteLayoutFlags.All);
            /*I add opacity so that it looks more like a popup,change background to Transparent if you want this to look more like a true pop up */
            BackgroundColor = Color.White;
            Opacity = 0.77;
            /* All of this content is optional, apply whatever you wish to your PopUp */
            Content = new StackLayout
            {
                /* */
                VerticalOptions = LayoutOptions.Center,
                HorizontalOptions = LayoutOptions.Center,
                /* */
                Children = {
                    /* */
                    new Image {
                        Source = "logo.png",
                        Aspect = Aspect.AspectFill,
                        Margin = new Thickness(100,30,100,20)
                    },
                    /* */
                    new Label {
                        Text = "Espera...",
                        TextColor = Color.Black,
                        HorizontalOptions = LayoutOptions.Center,
                        VerticalOptions = LayoutOptions.Center,
                        VerticalTextAlignment = TextAlignment.Center
                    },
                }
            };
            //Optional, do not use if you do not understand animations
            RotateLogo();
        }
        private void RotateLogo()
        {
            //uint duration = 10 * 60 * 1000;
            var sLayout = this.Content as StackLayout;
            var logo = sLayout.Children[0] as Image;
            logo.RotateYTo(4 * 360, 10000);
        }
    }
Example.xaml文件将与WaitPopup一起使用,请记住,从技术上讲,您可以使此弹出窗口与任何布局兼容,但在本例中,我们假设每个页面都有一个AbsoluteLayout作为其内容,并且当我们添加WaitPopup.cs时,它是AbsoluteLayout的子数组中的第二个子项:

<?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:d="http://xamarin.com/schemas/2014/forms/design"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             mc:Ignorable="d"
             x:Class="CALogin.Views.LoginPage"
             xmlns:vm="clr-namespace:CALogin.ViewModels">
    <ContentPage.BindingContext>
        <vm:LoginViewModel/>
    </ContentPage.BindingContext>
    <ContentPage.Content>
        <AbsoluteLayout x:Name="MasterLayout" VerticalOptions="FillAndExpand"
                    HorizontalOptions="FillAndExpand" Padding="0">
            <Grid x:Name="MasterGrid" BackgroundColor="Gray">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="*" />
                </Grid.ColumnDefinitions>
                <Grid.RowDefinitions>
                    <RowDefinition Height="*" />
                </Grid.RowDefinitions>
                <!-- Place all of your normal page content here, inside this "MasterGrid" -->
                <!-- When you call ShowPopup(), WaitPopup will be added as the 2nd child of the AbsoluteLayout -->
                <!-- WaitPopup will fill and expand the entire screen, overlapping MasterGrid -->
            </Grid>
        </AbsoluteLayout>
    </ContentPage.Content>
</ContentPage>

如果要添加ViewModel,可以在WaitPopup.CS中添加BindingContext


干杯

根据您的描述,您想在不使用插件的情况下创建弹出窗口,我使用Collectionview创建自定义弹出窗口,并将isvisible设置为false default,您可以查看:

MainPage.xaml:

 <ContentPage.Content>
    <AbsoluteLayout
        Padding="0"
        HorizontalOptions="FillAndExpand"
        VerticalOptions="FillAndExpand">
        <StackLayout
            AbsoluteLayout.LayoutBounds="0, 0, 1, 1"
            AbsoluteLayout.LayoutFlags="All"
            BackgroundColor="Azure">
            <StackLayout HorizontalOptions="CenterAndExpand" VerticalOptions="CenterAndExpand">
                <Label
                    FontAttributes="Bold"
                    FontSize="Medium"
                    HorizontalOptions="Center"
                    Text="Xamarin Monkeys" />
                <Image
                    x:Name="imgMonkey"
                    HeightRequest="200"
                    Source="a11.jpg"
                    WidthRequest="200" />
                <Button
                    Clicked="btnPopupButton_Clicked"
                    HorizontalOptions="Center"
                    Text="Show Popup"
                    VerticalOptions="Center" />
            </StackLayout>
        </StackLayout>

        <!--  Popup Area  -->
        <ContentView
            x:Name="popupLoginView"
            Padding="10,0"
            AbsoluteLayout.LayoutBounds="0, 0, 1, 1"
            AbsoluteLayout.LayoutFlags="All"
            BackgroundColor="#C0808080"
            IsVisible="false">

            <StackLayout HorizontalOptions="Center" VerticalOptions="Center">
                <StackLayout
                    BackgroundColor="White"
                    HeightRequest="200"
                    Orientation="Vertical"
                    WidthRequest="300">
                    <Entry Margin="20,20,20,10" Placeholder="Enter Username" />
                    <Entry Margin="20,0,20,0" Placeholder="Enter Password" />
                    <Button Margin="20,0,20,0" Text="Login" />

                </StackLayout>

            </StackLayout>

        </ContentView>
    </AbsoluteLayout>

private void btnPopupButton_Clicked(object sender, EventArgs e)
    {
        popupLoginView.IsVisible = true;
    }


您可以使用Collectionview在不同的contentpage中创建不同的弹出窗口。

根据您的描述,您希望在不使用插件的情况下创建弹出窗口,我使用Collectionview创建自定义弹出窗口,并将isvisible设置为false默认值,您可以查看:

MainPage.xaml:

 <ContentPage.Content>
    <AbsoluteLayout
        Padding="0"
        HorizontalOptions="FillAndExpand"
        VerticalOptions="FillAndExpand">
        <StackLayout
            AbsoluteLayout.LayoutBounds="0, 0, 1, 1"
            AbsoluteLayout.LayoutFlags="All"
            BackgroundColor="Azure">
            <StackLayout HorizontalOptions="CenterAndExpand" VerticalOptions="CenterAndExpand">
                <Label
                    FontAttributes="Bold"
                    FontSize="Medium"
                    HorizontalOptions="Center"
                    Text="Xamarin Monkeys" />
                <Image
                    x:Name="imgMonkey"
                    HeightRequest="200"
                    Source="a11.jpg"
                    WidthRequest="200" />
                <Button
                    Clicked="btnPopupButton_Clicked"
                    HorizontalOptions="Center"
                    Text="Show Popup"
                    VerticalOptions="Center" />
            </StackLayout>
        </StackLayout>

        <!--  Popup Area  -->
        <ContentView
            x:Name="popupLoginView"
            Padding="10,0"
            AbsoluteLayout.LayoutBounds="0, 0, 1, 1"
            AbsoluteLayout.LayoutFlags="All"
            BackgroundColor="#C0808080"
            IsVisible="false">

            <StackLayout HorizontalOptions="Center" VerticalOptions="Center">
                <StackLayout
                    BackgroundColor="White"
                    HeightRequest="200"
                    Orientation="Vertical"
                    WidthRequest="300">
                    <Entry Margin="20,20,20,10" Placeholder="Enter Username" />
                    <Entry Margin="20,0,20,0" Placeholder="Enter Password" />
                    <Button Margin="20,0,20,0" Text="Login" />

                </StackLayout>

            </StackLayout>

        </ContentView>
    </AbsoluteLayout>

private void btnPopupButton_Clicked(object sender, EventArgs e)
    {
        popupLoginView.IsVisible = true;
    }


您可以使用Collectionview在不同的contentpage中创建不同的弹出窗口。

如果可能的话,我喜欢这样,尽管这个弹出窗口只能在它存在的同一个页面中显示。@LISA,有什么更新吗?如果我的答复对您有帮助,请记住将我的答复标记为“答复”,谢谢。@CherryBu MSFT您是指ContentView还是CollectionView?如果可能的话,我喜欢这样做,尽管这个弹出窗口只能显示在它存在的同一页中。@LISA,有什么更新吗?如果我的回答对您有帮助,请记住将我的回答标记为“谢谢”。@CherryBu MSFT您是指ContentView还是CollectionView?