Xamarin 页面完全加载时的事件? [英] Event when Xamarin page is FULLY loaded?

查看:110
本文介绍了Xamarin 页面完全加载时的事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

社区.

事先,如果这真的有一个简单的解决方案,我深表歉意.考虑到我在 Xamarin 方面的技能水平,我遇到了一个棘手的问题,需要解决.

问题是当页面完全显示时,Xamarin 似乎没有要调用的事件/函数/等,因为您可以看到页面应该显示的所有内容,然后调用该函数.快速说明, 对我不起作用的是 OnAppearing 函数,因为它在屏幕可见之前过早地触发.

我试图在此处破解解决方案.在这篇博文中,用户使用以下代码进行回答.

这是第 1) 步:

private bool _toggleTemp;public bool ToggleTemp{得到 =>_toggleTemp;设置 =>SetProperty(ref _toggleTemp, value);}

步骤 2)

LoadingVm.ToggleTemp = true;

步骤 3)

步骤 4)

private async void Switch_OnToggled(object sender, ToggledEventArgs e){/* 你的代码在这里... */}

第一个问题.我创建了一个 ViewModel 文件,它位于名为ViewModels"的文件夹内,在链接中发布代码的用户说要在视图模型中创建一个属性,由于某种原因,我收到一条错误消息,指出名称集属性在当前上下文中不存在".那么,我可以换出该代码并仅输入以下内容吗?我的意思是它看起来像同一件事,对吗?

public bool Temp{得到{返回_toggleTemp;}设置 { _toggleTemp = 值;}}

第二个问题.我不知道什么是LoadingVm"在他的代码中.它不适合我.是不是因为我缺少一个使用"在顶部?但无论如何,整个代码行都是LoadingVm.ToggleTemp = true;",所以他只是调用函数将其设置为true.

最后,我假设这无疑会帮助我让代码工作,在页面完全加载后我可以做任何我想做的事情,正确的?我真的没有看到太多人不同意链接中的人提供的方法,但如果是这样,请随时提供其他建议.

再次,如果这是一个简单的错误,请原谅我.我仍然习惯了 Xamarin,而且由于有一段时间没有使用 C#,我对 C# 的印象仍然有些新鲜.

解决方案

您可以使用 ContentPage 的自定义渲染器来了解视图是否已完全加载,然后使用 MessagingCenter 通知您的共享项目:

在 iOS 中:

[assembly:ExportRenderer (typeof(ContentPage), typeof(CameraPageRenderer))]命名空间 App362.iOS{公共类 CameraPageRenderer : PageRenderer{公共覆盖无效 ViewDidAppear(布尔动画){base.ViewDidAppear(动画);Console.WriteLine(ViewDidAppear");MessagingCenter.Send(new object(),ViewLoaded");}}}

在安卓中:

[程序集:ExportRenderer(typeof(ContentPage), typeof(CameraPageRenderer))]命名空间 App362.Droid{[过时的]公共类 CameraPageRenderer : PageRenderer{受保护的覆盖 void OnAttachedToWindow(){base.OnAttachedToWindow();Console.WriteLine(OnAttachedToWindow");MessagingCenter.Send(new object(),ViewLoaded");}}}

在共享项目中:

公共部分类 MainPage : ContentPage{公共主页(){初始化组件();}受保护的覆盖 void OnAppearing(){base.OnAppearing();Console.WriteLine(OnAppearing");MessagingCenter.Subscribe<object>(new object(), ViewLoaded", (sender) =>{//每当ViewLoaded"出现时做一些事情收到消息Console.WriteLine(每当收到 ViewLoaded 消息时就做点什么");});}}

community.

In advance, I apologize if this really has a simple solution. I have a troublesome problem, considering my skill level with Xamarin, that I need to get resolved.

The problem is it seems like Xamarin doesn't have an event/function/etc to call when a page is FULLY displayed, as in you can see everything the page is supposed to display, and then that function is called. Quick note, what does not work for me is the OnAppearing function because it fires off too early before the screen is visible.

I was attempting to decipher a solution here. In this post, a user answers with the following code.

Here is step 1):

private bool _toggleTemp;
public bool ToggleTemp
{
    get => _toggleTemp;
    set => SetProperty(ref _toggleTemp, value);
}

Step 2)

LoadingVm.ToggleTemp = true;

Step 3)

<Switch IsToggled="{Binding ToggleTemp}" Toggled="Switch_OnToggled" IsVisible="False" />

Step 4)

private async void Switch_OnToggled(object sender, ToggledEventArgs e)
{ 
    /* Your code goes here... */ 
}

First concern. I created a ViewModel file, it's inside of a folder called "ViewModels", and the user who posted the code in the link said to create a property in the view model, for some reason I'm getting an error stating "the name set property does not exist in the current context". So is it alright I swap that code out and just put the following instead? I mean it SEEMS like the same thing, right?

public bool Temp
{
    get { return _toggleTemp; }
    set { _toggleTemp = value; }
}

Second concern. I have no clue what "LoadingVm" is in his code. It doesn't come up for me. Is it because I'm missing a "using" at the top? But regardless, the whole code line was this "LoadingVm.ToggleTemp = true;", so he's just calling the function to set it to be true.

In the end, I'm ASSUMING that this will no doubt help me get the code working where I can do whatever I please AFTER the page is completely loaded, correct? I don't really see too many people disagreeing with the method the guy in the link has given, but if so, feel free to give other suggestions.

Again, please forgive me if this is a simple error. I'm still getting used to Xamarin and C# is still somewhat fresh in my mind from not having used it in a while.

解决方案

You can use custom renderer of ContentPage to know if the view is fully loaded and then use MessagingCenter to notify your shared project:

In iOS:

[assembly:ExportRenderer (typeof(ContentPage), typeof(CameraPageRenderer))]
namespace App362.iOS
{
    public class CameraPageRenderer : PageRenderer
    {

        public override void ViewDidAppear(bool animated)
        {
            base.ViewDidAppear(animated);

            Console.WriteLine("ViewDidAppear");

            MessagingCenter.Send<object>(new object(), "ViewLoaded");

        }

    }     
}

In Android:

[assembly: ExportRenderer(typeof(ContentPage), typeof(CameraPageRenderer))]
namespace App362.Droid
{
    [Obsolete]
    public class CameraPageRenderer : PageRenderer{

        protected override void OnAttachedToWindow()
        {
            base.OnAttachedToWindow();

            Console.WriteLine("OnAttachedToWindow");

            MessagingCenter.Send<object>(new object(), "ViewLoaded");
        }
    }

}

In Shared project:

public partial class MainPage : ContentPage
{
    public MainPage()
    {
        InitializeComponent();
    }

    protected override void OnAppearing()
    {
        base.OnAppearing();
        Console.WriteLine("OnAppearing");

        MessagingCenter.Subscribe<object>(new object(), "ViewLoaded", (sender) =>
        {
            // Do something whenever the "ViewLoaded" message is received
            Console.WriteLine("Do something whenever the ViewLoaded message is received");
        });
    }
}

这篇关于Xamarin 页面完全加载时的事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆