包含 Web 服务调用的 Xamarin PCL 初始屏幕

标签 xamarin xamarin.forms

我想要一个显示欢迎消息然后从网络服务加载数据的 Xamarin PCL ContentPage。我的 Web 服务正在运行,但问题是在下面的示例中,它需要一些时间才能运行,但没有显示任何内容。

一般策略是什么:显示等待类型的启动页面,等待长时间运行的任务完成,然后继续应用程序的其余部分(比如主菜单页面)?

希望该策略能在 PCL 项目中发挥作用,这样我就不必尝试编写 iOS 和 Android 版本了?我试图将代码放入各种覆盖方法中,但似乎没有任何效果。在此先感谢您的任何建议。

// The following is called from the "App : Application" class.
public class SplashScreen : ContentPage
{
    public SplashScreen()
    {
        Label lblWelcome = new Label { Text = "Hello. Please wait..." };

        Content = new StackLayout
        {
            Children = { lblWelcome }
        }

        //TODO: Have the device display the above content before the following continues...

        CallWebServiceToLoadParameters();

        //TODO: Move on to display the Main Menu
    }
}

最佳答案

这可能会成功:

public class SplashScreen : ContentPage
{
    public SplashScreen()
    {
        Label lblWelcome = new Label { Text = "Hello. Please wait..." };

        Content = new StackLayout
        {
            Children = { lblWelcome }
        }

        Device.BeginInvokeOnMainThread(async () => {
            //If awaitable
            var response = await CallWebServiceToLoadParameters();

            if(response{
                App.Current.MainPage = //Your main page.
            }
        });

    }
}

有点难看,正确的方法是为每个页面使用一个 ViewModel。

关于包含 Web 服务调用的 Xamarin PCL 初始屏幕,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40100534/

相关文章:

c# - 如何在 Xamarin.Forms 中重用父 ContentView 中定义的样式

c# - 如何在 Xamarin.Forms 中设置 android id 属性?

button - Xamarin Forms DisplayAlert 按钮 TextColor

c# - 具有透明状态栏的 Xamarin Forms 背景图像

c# - 漏洞? System.ArgumentException : 'unable to figure out route for:

c# - MvvmCross 与 Xamarin.Forms : Setup not called

xamarin - Xamarin 项目没有 PCL 选项

c# - 在 Xamarin Mac 中阻止鼠标气泡

android - MvvmCross View模型生命周期

C# - Xamarin 表单在切换时删除 ListView 项