Android/WASM : prismMvvm:ViewModelLocator.AutoWireViewModel=“True” is not Wire-up the ViewModel for Android and Wasm

匆匆过客 提交于 2020-06-01 07:38:25

问题


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();
            }
        }
    }
}

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


回答1:


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:

  • Change the UserControl to a ContentControl
  • Add the following property in your csproj:
<UnoSkipUserControlsInVisualTree>false</UnoSkipUserControlsInVisualTree>

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.



来源:https://stackoverflow.com/questions/61570953/android-wasm-prismmvvmviewmodellocator-autowireviewmodel-true-is-not-wire-u

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!