Android/WASM:prismMvvm:ViewModelLocator.AutoWireViewModel=“True"不是为 Android 和 Wasm 连接 ViewModel [英] Android/WASM : prismMvvm:ViewModelLocator.AutoWireViewModel="True" is not Wire-up the ViewModel for Android and Wasm

查看:104
本文介绍了Android/WASM:prismMvvm:ViewModelLocator.AutoWireViewModel=“True"不是为 Android 和 Wasm 连接 ViewModel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在我的视图中使用prismMvvm:ViewModelLocator.AutoWireViewModel="True" 自动连接ViewModel.对于 UWp,它工作得很好.但是在 Android 和 WASM 中,View 无法使用 Prism 在我的 Uno 平台应用程序中连接 ViewModel.

I am trying to auto-wire up the ViewModel using prismMvvm:ViewModelLocator.AutoWireViewModel="True" in my View. For UWp it working perfectly. But with-in Android and WASM, View not able to wire up the ViewModel in my Uno platform Application using Prism.


<UserControl
    x:Class="RepayablClient.Shared.Views.Login"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:prismMvvm="using:Prism.Mvvm"
    HorizontalAlignment="Stretch"
    VerticalAlignment="Stretch"
    prismMvvm:ViewModelLocator.AutoWireViewModel="True"
    mc:Ignorable="d">
    <Grid Background="#881798">
        <Grid.RowDefinitions>
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>
        <Grid
            Width="250"
            Height="300"
            HorizontalAlignment="Center"
            VerticalAlignment="Center">
            <Grid.RowDefinitions>
                <RowDefinition Height="*" />
                <RowDefinition Height="25" />
                <RowDefinition Height="25" />
            </Grid.RowDefinitions>
            <Image
                Grid.Row="0"
                Source="/Assets/Icon.png"
                Stretch="Fill" />
            <TextBlock Grid.Row="1" Text="{Binding LoginUser}" />
            <ProgressBar
                Grid.Row="2"
                Width="250"
                Margin="0,20,0,0"
                Foreground="White"
                IsIndeterminate="True"
                ShowError="False"
                ShowPaused="False" />
        </Grid>
    </Grid>
</UserControl>

using Microsoft.Identity.Client;
using RepayablClient.ViewModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace RepayablClient.Shared.ViewModels
{
    public class LoginViewModel : ViewModelBase
    {
        //public ICommand LoginCommand { get; set; }
        string graphAPIEndpoint = "https://graph.microsoft.com/v1.0/me";
        private string _loginUser;

        public string LoginUser
        {
            get { return _loginUser; }
            set
            {
                _loginUser = value;
                RaisePropertyChanged();
            }
        }
        public LoginViewModel()
        {
            Title = "Login Page";
            LoginUser = "Attempt to Login";
            _ = LoginCommandExecutedAsync();
            //LoginCommand = new AsyncCommand(LoginCommandExecutedAsync);
        }

        private async Task LoginCommandExecutedAsync()
        {

            AuthenticationResult authResult = null;
            IEnumerable<IAccount> accounts = await App.publicClientApplication.GetAccountsAsync().ConfigureAwait(false);
            IAccount firstAccount = accounts.FirstOrDefault();
            try
            {
                authResult = await App.publicClientApplication.AcquireTokenSilent(Consts.Scopes, firstAccount)
                                                        .ExecuteAsync();
            }
            catch (MsalUiRequiredException ex)
            {
                System.Diagnostics.Debug.WriteLine($"MsalUiRequiredException: {ex.Message}");
                try
                {
                    authResult = await App.publicClientApplication.AcquireTokenInteractive(Consts.Scopes)
                       .ExecuteAsync();
                }
                catch (MsalException msalex)
                {
                    // await DisplayMessageAsync($"Error Acquiring Token:{System.Environment.NewLine}{msalex}");
                }
            }
            catch
            {
                //  await DisplayMessageAsync($"Error Acquiring Token Silently:{System.Environment.NewLine}{ex}");
                return;
            }
            if (authResult != null)
            {
                var content = await GetHttpContentWithTokenAsync(graphAPIEndpoint,
                                                            authResult.AccessToken).ConfigureAwait(false);

                LoginUser = content;

            }
        }
        public async Task<string> GetHttpContentWithTokenAsync(string url, string token)
        {
            var httpClient = new System.Net.Http.HttpClient();
            System.Net.Http.HttpResponseMessage response;
            try
            {
                var request = new System.Net.Http.HttpRequestMessage(System.Net.Http.HttpMethod.Get, url);
                // Add the token in Authorization header
                request.Headers.Authorization =
                  new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", token);
                response = await httpClient.SendAsync(request);
                var content = await response.Content.ReadAsStringAsync();
                return content;
            }
            catch (Exception ex)
            {
                return ex.ToString();
            }
        }
    }
}

有关详细信息,请访问我的存储库:https://github.com/avikeid2007/Repayabl

For more details visit my repo: https://github.com/avikeid2007/Repayabl

推荐答案

您正面临 an 在 Uno 中仍然存在的问题,将很快进行调整.基本上,如果您使用 UserControl,则在创建控件时可能不会考虑其中定义的某些属性.

You're facing an issue that is still standing in Uno, that will be adjusted soon. Basically, if you use a UserControl, some of the properties defined there may not be taken into account when the control is created.

您可以通过以下两种方式之一解决此问题:

You can fix this in one of two ways:

  • UserControl 更改为 ContentControl
  • 在您的 csproj 中添加以下属性:
<UnoSkipUserControlsInVisualTree>false</UnoSkipUserControlsInVisualTree>

这个问题是 Android 有一个非常短的 UI 线程堆栈空间的时代的残余,并且可视化树中的每一层都很重要.它不再那么重要了.

This issue is a remnant of a time where Android had a very short UI Thread stack space, and that every layer in the visual tree counted. It's not as critical anymore.

这篇关于Android/WASM:prismMvvm:ViewModelLocator.AutoWireViewModel=“True"不是为 Android 和 Wasm 连接 ViewModel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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