程序问答   发布时间:2022-06-01  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了将查询参数添加到 Xamarin Forms Shell 路由大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决将查询参数添加到 Xamarin Forms SHell 路由?

开发过程中遇到将查询参数添加到 Xamarin Forms SHell 路由的问题如何解决?下面主要结合日常开发的经验,给出你关于将查询参数添加到 Xamarin Forms SHell 路由的解决方法建议,希望对你解决将查询参数添加到 Xamarin Forms SHell 路由有所启发或帮助;

(也在微软论坛上发布了这个问题)

类似于 How do I add a route query parameter to a SHellContent item in a Xamarin Forms app ,但该线程对我没有帮助。

我最近开始了一个新项目,并决定使用 Xamarin Forms SHell。

我有一个内容页面,它根据提供的查询参数表现出不同的行为。 有三个选项卡项将路由到同一内容页面,每个选项卡项提供不同的查询参数。

我不知道如何通过 AppSHell.xaml 路由将此参数传递给我的内容页面视图模型。

请参阅下面我复制要求的示例代码。

在这个示例中,我想使用 xaml 路由中提供的查询参数设置内容页面标签。 使用下面的代码,我得到一个空引用异常。

AppSHell.xaml:

<FlyoutItem title="Sample">
     <Tab title="Red">
         <SHellContent Route="SamplePage?Parameter=Red" ContentTemplate="{DataTemplate sample:SamplePagE}" />
     </Tab>
     <Tab title="Blue">
         <SHellContent Route="SamplePage?Parameter=Blue" ContentTemplate="{DataTemplate sample:SamplePagE}" />
     </Tab>
     <Tab title="Green">
         <SHellContent Route="SamplePage?Parameter=Green" ContentTemplate="{DataTemplate sample:SamplePagE}" />
     </Tab>
 </FlyoutItem>

SamplePageVM:

[queryProperty(nameof(Parameter),nameof(Parameter))]
public class SamplePageVM
{
    public String Parameter { get; set; }
}

SamplePage.xaml:

<ContentPage.Content>
     <GrID>
         <Label Text="{Binding Parameter}"/>
     </GrID>
 </ContentPage.Content>

任何帮助将不胜感激。 或者替代方法来解决这个问题。

非常感谢。

*编辑

以下是我目前的解决方法。

AppSHell.xaml:

<FlyoutItem title="Sample">
    <Tab title="Red">
        <SHellContent Route="RedRoute" ContentTemplate="{DataTemplate sample:SamplePagE}" />
    </Tab>
    <Tab title="Blue">
        <SHellContent Route="BlueRoute" ContentTemplate="{DataTemplate sample:SamplePagE}" />
    </Tab>
    <Tab title="Green">
        <SHellContent Route="GreenRoute" ContentTemplate="{DataTemplate sample:SamplePagE}" />
    </Tab>
</FlyoutItem>

AppSHell.xaml.cs

protected overrIDe voID OnNavigaTing(SHellNavigaTingEventArgs args)
    {
        base.onNavigaTing(args);

        if (args.Target.LOCATIOn.originalString.Tolower().Contains("redroute"))
        {
            StaticHelper.Parameter = "Red";
        }
        else if (args.Target.LOCATIOn.originalString.Tolower().Contains("blueroute"))
        {
            StaticHelper.Parameter = "Blue";
        }
        else if (args.Target.LOCATIOn.originalString.Tolower().Contains("greenroute"))
        {
            StaticHelper.Parameter = "Green";
        }
    }

SamplePageVM:

public class SamplePageVM
{
    public String Parameter { get; set; }

    public SamplePageVM()
    {
        Parameter = StaticHelper.Parameter;
    }
}

SamplePage.xaml:

<ContentPage.Content>
    <GrID>
        <Label Text="{Binding Parameter}"/>
    </GrID>
</ContentPage.Content>

可能不是最佳实践,但确实如此。

解决方法

我可以重现这个异常。检查屏幕截图:https://imgur.com/HNmjewa

System.NullReferenceException: 'Object reference not set to an instance of an object.'

像下面这样更改 Route 将修复此异常。

<FlyoutItem title="Sample">
    <Tab title="Red">
        <SHellContent ContentTemplate="{DataTemplate sample:SamplePagE}" Route="Red" />
    </Tab>
    <Tab title="Blue">
        <SHellContent ContentTemplate="{DataTemplate sample:SamplePagE}" Route="Blue" />
    </Tab>
    <Tab title="Green">
        <SHellContent ContentTemplate="{DataTemplate sample:SamplePagE}" Route="Green" />
    </Tab>
</FlyoutItem>

如果您想使用查询参数,请使用包含所有三个组件的 URI 调用 GoToAsync 方法,结构为://route/page?queryParameters

String monkeyName = (e.CurrentSELEction.FirstOrDefault() as Animal).Name;
        // The following route works because route names are unique in this application.
        await SHell.Current.GoToAsync($"routename?Parameter={monkeyNamE}");
        //routename is the String of the Route in xaml or register the code: RoutIng.RegisterRoute
       

有关更多详细信息,您可以从下面的链接下载代码示例。 https://docs.microsoft.com/en-us/samples/xamarin/xamarin-forms-samples/userinterface-xaminals/

大佬总结

以上是大佬教程为你收集整理的将查询参数添加到 Xamarin Forms Shell 路由全部内容,希望文章能够帮你解决将查询参数添加到 Xamarin Forms Shell 路由所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签:路由