在构造函数VB.NET中设置共享属性值为Me

本文关键字:属性 Me 共享 设置 构造函数 VB NET | 更新日期: 2023-09-27 18:17:48

我尝试创建一个VB。. NET版本的Template10 UWP模板。

模板几乎准备好了,但在执行后,我在Shell.xaml.vb文件中得到一个错误消息。该类的原始c#代码:

public sealed partial class Shell : Page
{
    public static Shell Instance { get; set; }
    public static HamburgerMenu HamburgerMenu => Instance.MyHamburgerMenu;
    Services.SettingsServices.SettingsService _settings;
    public Shell()
    {
        Instance = this;
        InitializeComponent();
        _settings = Services.SettingsServices.SettingsService.Instance;
    }
    public Shell(INavigationService navigationService) : this()
    {
        SetNavigationService(navigationService);
    }
    public void SetNavigationService(INavigationService navigationService)
    {
        MyHamburgerMenu.NavigationService = navigationService;
        HamburgerMenu.RefreshStyles(_settings.AppTheme, true);
        HamburgerMenu.IsFullScreen = _settings.IsFullScreen;
        HamburgerMenu.HamburgerButtonVisibility = _settings.ShowHamburgerButton ? Visibility.Visible : Visibility.Collapsed;
    }
}

和在VB中转换的版本。净:

Partial Public NotInheritable Class Shell
    Inherits Page
    Public Shared Property Instance As Shell
    Public Shared Property HamburgerMenu As HamburgerMenu = Instance.MyHamburgerMenu
    Public _settings As SampleApp.Services.SettingsServices.SettingsService
    Public Sub New()
        Instance = Me
        InitializeComponent()
        _settings = SampleApp.Services.SettingsServices.SettingsService.Instance
    End Sub
    Public Sub New(navigationService As INavigationService)
        Me.New()
        SetNavigationService(navigationService)
    End Sub
    Public Sub SetNavigationService(navigationService As INavigationService)
        MyHamburgerMenu.NavigationService = navigationService
        HamburgerMenu.RefreshStyles(_settings.AppTheme, True)
        HamburgerMenu.IsFullScreen = _settings.IsFullScreen
        HamburgerMenu.HamburgerButtonVisibility = If(_settings.ShowHamburgerButton, Visibility.Visible, Visibility.Collapsed)
    End Sub
End Class

执行的入口点是:

Public Sub New(navigationService As INavigationService)

,异常出现在这一行:

 Instance = Me

异常消息:

类型为'System '的异常。TypeInitializationException'在SampleApp.exe中发生,但未在用户代码中处理

附加信息:SampleApp.Views的类型初始化器。Shell抛出了一个异常。

InnerException: Object reference not set to a instance of Object .

我如何使这个VB。NET类工作吗?谢谢!

在构造函数VB.NET中设置共享属性值为Me

您对'HamburgerMenu'属性的转换不正确。用途:

Public Shared ReadOnly Property HamburgerMenu() As HamburgerMenu
    Get
        Return Instance.MyHamburgerMenu
    End Get
End Property

最初的c#为'HamburgerMenu'使用了一个'表达式体'成员-这个特性在VB中是不可用的。