C # + XAML - user interface was not updated after changes

I want to update my interface, but it did not update even when the event was fired PropertyChanged. When I start, I get the first values, but after the changes, it will not be updated.

During debugging, I see that the values ​​receiving the update and the event PropertyChangedwere fired, but the receiver did not receive the call.

(BTW: I'm relatively new to C #, respectively, in the concept of MV-VM.)

Xaml

<Page.DataContext>
    <vm:MainPageViewModel x:Name="ViewModel" />
</Page.DataContext>
...
<TextBlock x:Name="LatitudeVar" Text="{Binding LatitudeVar, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Width="79" Canvas.Left="189" Canvas.Top="38" />

C # (MainPageViewModel)

public class MainPageViewModel : ViewModelBase, INotifyPropertyChanged
{
    private Session _session;
    public MainPageViewModel()
    {
        _session = Session.Instance;
    }

    public static readonly MainPageViewModel Instance = new MainPageViewModel();

    public override async Task OnNavigatedToAsync(object parameter, NavigationMode mode, IDictionary<string, object> suspensionState)
    {

        // App just started, so we get GPS access and eventually initialize the client
        Session.InitializeClient();
        await StartGpsDataService();
        await Task.CompletedTask;
    }

    ...

    private string _latitude;
    public string LatitudeVar
    {
        get { return _session.Latitude; }
        set { _session.Latitude = value; NotifyPropertyChanged(); }
    }

    ...

    public async Task StartGpsDataService()
    {
        await Session.InitializeDataUpdate();
    }

     public new event PropertyChangedEventHandler PropertyChanged;

    [Annotations.NotifyPropertyChangedInvocator]
    protected virtual void NotifyPropertyChanged([CallerMemberName] string propertyName = null)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }

C # (session)

public class Session : INotifyPropertyChanged
{
    public static void InitializeClient()
    {
        MainPageViewModel mpvm = new MainPageViewModel();
        _mpvm = MainPageViewModel.Instance;
    }

    private static Session _instance;
    public static Session Instance
    {
        get
        {
            if (_instance == null)
            {
                _instance = new Session();
            }
            return _instance;
        }
    }


    private static Session _sc;
    internal static Session Sc
    {
        get { return _sc; }
        set { _sc = value; }
    }


    private static MainPageViewModel _mpvm;

    private string _latitude;
    public string Latitude
    {
        get { return _latitude; }
        set
        {
            if (_latitude == value) return; _latitude = value; RaiseOnPropertyChanged("Latitude"); }
    }

    ...

     public void UpdateGpsData(Geopoint point, Geopoint geopointOld)
    {
        _mpvm.LatitudeVar = point.Position.Latitude.ToString();
    }

    public static async Task InitializeDataUpdate()
    {
       Sc = Session.Instance;
       Sc.StartTime = DateTime.Now;
       Sc.GetPosition(Geoposition.Coordinate.Point);
    }

    public new event PropertyChangedEventHandler PropertyChanged;

    [NotifyPropertyChangedInvocator]
    protected virtual void RaiseOnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }

Some ways I tried before:

C # (MainPageViewModel)

private string _latitude;
public string LatitudeVar
{
    get { return _latitude; }
    set { _latitude = value; NotifyPropertyChanged("LatitudeVar"); }
}

Result . Values ​​were not displayed.

C # (session)

public void UpdateGpsData(Geopoint point, Geopoint geopointOld)
{
    Latitude = point.Position.Latitude.ToString();
}

Result . The values ​​were filled at startup, but not updated.

EDIT: : , ;) Session.cs - . relavant ViewModel. , :

public MainPageViewModel()
{
   _session = Session.Instance;
   _instance = this;
}
+4
3

Woah, .

-, MainPageViewModel, Session.InitializeClient, , . XAML

<Page.DataContext>
    <vm:MainPageViewModel x:Name="ViewModel" />
</Page.DataContext>

MainPageViewModel,

public void UpdateGpsData(Geopoint point, Geopoint geopointOld)
{
    _mpvm.LatitudeVar = point.Position.Latitude.ToString();
}

.

, , , readonly MainPageViewModel :

<TextBlock Text="{Binding Path=Session.Latitude, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>

, , , , , INotifyPropertyChanged.

, / .

+4

, Caliburn Micro IoC . .

:

  • LocationService IEventAggregator Initialize. Initialize, , () LocationChanged, , IEventAggregator.

  • ShellViewModel IEventAggregator , IHandle<LocationChanged>. OnActivate ( Caliburn Micro , ), ShellViewModel Subscribe IEventAggregator, this . IEventAggregator , IHandle<> , , , generic ( LocationChanged), Handle ( ShellViewModel) . Handle ShellViewModel Dispatcher , Location, , .

  • ShellView Location Latitude (Location.Y) (Location.X) .

  • AppBootstrapper IEventAggregator ILocationService Singleton IoC ( Caliburn SimpleContainer). , - (-) . , OnStartup ILocationService , LocationChange DisplayRootViewFor<IShell> ( , Caliburn ShellViewModel, , ShellView.

, , . ShellViewModel ILocationService - () .

, .

+2

Isn't that a simple singleton?

public class MyViewModel
{
    static MyViewModel()
    {
        Instance = new MyViewModel();
    }

    public static MyViewModel Instance { get; }

    private MyViewModel()
    {
        // TODO
    }

    public override async Task OnNavigatedToAsync(object parameter, NavigationMode mode, IDictionary<string, object> suspensionState)
    {
        await Task.CompletedTask;
    }
}
0
source

Source: https://habr.com/ru/post/1653511/


All Articles