1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

PrismのBindableBase.SetPropertyとNullable型と.net nativeコンパイルとが組み合わさると発生する問題への対処方法

1
Posted at

概要

例えばBindableBaseを継承したクラスに以下のような変更通知対応したNullableなプロパティがあった時に、

private TimeSpan? _TimeshiftPosition;
public TimeSpan? TimeshiftPosition
{
    get { return _TimeshiftPosition; }
    set { SetProperty(ref _TimeshiftPosition, value); }
}

リリースビルド(Native Tool chainコンパイルを有効に)して動かすと、setter呼び出し時に

ハンドルされない例外が 0x0A7E9F08 (SharedLibrary.dll) で発生しました(NicoPlayerHohoema.exe 内): 0x00001007。

という例外が発生してアプリがクラッシュします。

対処方法

Xamarin.Forms UWP app failing at run time with .net native tool chain but works otherwise
https://stackoverflow.com/questions/55867384/xamarin-forms-uwp-app-failing-at-run-time-with-net-native-tool-chain-but-works

こちらのStackoverflowの回答によると

So this turned out to be an issue with Prism (7.0.0.396) and the .net native. It looks like binding properties with a nullable type (int? in this case) using BindableBase.SetProperty fail when compiled with the .net native tool chain. The following appears to fix this:

つまり .net native と Prism における問題であり、Nullable型に対してBindableBase.SetPropertyを通じてプロパティ変更通知を行おうとすると、.net native tool chain でコンパイルした時に問題が発生するということのようです。

問題を回避するには以下のクラスをBindableBaseと差し替えて利用します。

public class BindableBaseWithFix : BindableBase
{
    protected virtual bool SetProperty<T>(ref T? storage, T? value, [CallerMemberName] string propertyName = null)
        where T : struct
    {
        if (EqualityComparer<T?>.Default.Equals(storage, value))
            return false;
        storage = value;
        RaisePropertyChanged(propertyName);
        return true;
    }
} 

試行環境

  • C# 7.3
  • Prism 7.2.0
  • UWP
    • ターゲットバージョン version 1803 (10.0; ビルド 17134)
    • 最小バージョン Faill Creators Update (10.0; ビルド 16299)
1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?