【问题标题】:Transmit data on current xaml-page with Xamarin-Button使用 Xamarin-Button 在当前 xaml 页面上传输数据
【发布时间】:2020-06-30 13:53:25
【问题描述】:

我在 Xamarin.Forms 应用程序中有一个 Crousell 页面。您可以在此页面上的几张图片之间滑动。我现在想定义一个按钮,它在代码隐藏文件中执行一个操作。关键是,我想传递信息点击按钮时显示的图片

我将非常感谢有用的建议。

【问题讨论】:

    标签: android ios xamarin cross-platform


    【解决方案1】:

    关键是,我想在点击按钮的那一刻传递信息。

    正如 Jason 所说,CarouselPage 只是一个 ContentPages 的集合,您可以通过 ContentPage page = carousepages.CurrentPage; 获取当前的 contentpage。我做了一个样本,你可以看看:

    <CarouselPage
    x:Class="CarouselPageNavigation.MainPage"
    xmlns="http://xamarin.com/schemas/2014/forms"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
    x:Name="carousepages">
    <ContentPage x:Name="redcontentpage">
        <ContentPage.Padding>
            <OnPlatform x:TypeArguments="Thickness">
                <On Platform="iOS, Android" Value="0,40,0,0" />
            </OnPlatform>
        </ContentPage.Padding>
        <StackLayout>
            <Label
                FontSize="Medium"
                HorizontalOptions="Center"
                Text="Red" />
            <Button
                x:Name="btn1"
                Clicked="Btn1_Clicked"
                Text="get current page" />
            <BoxView
                x:Name="redbox"
                HeightRequest="200"
                HorizontalOptions="Center"
                VerticalOptions="CenterAndExpand"
                WidthRequest="200"
                Color="Red" />
        </StackLayout>
    </ContentPage>
    <ContentPage x:Name="greencontentpage">
        <ContentPage.Padding>
            <OnPlatform x:TypeArguments="Thickness">
                <On Platform="iOS, Android" Value="0,40,0,0" />
            </OnPlatform>
        </ContentPage.Padding>
        <StackLayout>
            <Label
                FontSize="Medium"
                HorizontalOptions="Center"
                Text="Green" />
            <Button
                x:Name="btn2"
                Clicked="Btn1_Clicked"
                Text="get current page" />
            <BoxView
                x:Name="greenbox"
                HeightRequest="200"
                HorizontalOptions="Center"
                VerticalOptions="CenterAndExpand"
                WidthRequest="200"
                Color="Green" />
        </StackLayout>
    </ContentPage>
    <ContentPage x:Name="nluecontentpage">
        <ContentPage.Padding>
            <OnPlatform x:TypeArguments="Thickness">
                <On Platform="iOS, Android" Value="0,40,0,0" />
            </OnPlatform>
        </ContentPage.Padding>
        <StackLayout>
            <Label
                FontSize="Medium"
                HorizontalOptions="Center"
                Text="Blue" />
            <Button
                x:Name="btn3"
                Clicked="Btn1_Clicked"
                Text="get current page" />
            <BoxView
                x:Name="bluebox"
                HeightRequest="200"
                HorizontalOptions="Center"
                VerticalOptions="CenterAndExpand"
                WidthRequest="200"
                Color="Blue" />
        </StackLayout>
    </ContentPage>
    

    可以通过以下代码获取当前Page BoxView:

     private  void Btn1_Clicked(object sender, System.EventArgs e)
        {
            ContentPage page = carousepages.CurrentPage;
            StackLayout stack = (StackLayout)page.Content;
            BoxView box = (BoxView)stack.Children[2];                 
    
    
        }
    

    【讨论】:

    • 感谢您的详细描述!效果很好。
    【解决方案2】:

    如果您使用的是CarouselView,这很容易。 CurrentItem 属性将返回当前选中的项目

    protected void ButtonClick(object sender, EventArgs e)
    {
      var item = (MyModel)MyCarouselView.CurrentItem;
    }
    

    【讨论】:

    • 感谢您的快速回复。不幸的是,我需要使用 CarouselPage。所以我正在寻找一个选项来使用 buttonClickedEvent 传递信息
    • 因为 CarouselPage 只是 ContentPages 的集合,每个页面都必须有自己的 Button 和代码隐藏,因此您可以硬编码值
    猜你喜欢
    • 2022-08-24
    • 2023-03-07
    • 2021-07-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多