0

我制作了一个小型 UWP 应用,它的 NavigationView 有一个设置页面。在其中,我制作了一个 Slider,它允许用户更改页面背景(或基本上是 NavigationView.Background)的亚克力不透明度(TintOpacity)。但我有一个错误

CS0120: An object reference is required for the non-static field, method or property 'MainPage.ChangeAcrylicOpacity(double)'

<!-- MainPage.xaml -->
<NavigationView
    x:Name="NavView"
    ItemInvoked="NavView_ItemInvoked"
    Loaded="NavView_Loaded"> <!-- and some other attributes -->
// MainPage.xaml.cs
public void ChangeAcrylicOpacity(double tintOpacity)
{
    AcrylicBrush acrylicBrush = NavView.Background as AcrylicBrush;
    acrylicBrush.TintOpacity = tintOpacity;
}

//-----------------------------------------------------------------------------------------------------------------

// Settings.xaml.cs
private void Slider_AcrylicValueChanged(object sender, RangeBaseValueChangedEventArgs e)
{
    Slider slider = sender as Slider;
    double tintOpacity = slider.Value;
    MainPage.ChangeAcrylicOpacity(tintOpacity);
}

错误在Settings.xaml.cs > Slider_AcrylicValueChanged > MainPage.ChangeAcrylicOpacity(tintOpacity);

当我MainPage.xaml.cs > ChangeAcrylicOpacity(double tintOpacity)从更改public为时出现此错误private,但出现错误'MainPage.ChangeAcrylicOpacity(double)' is inaccessible due to its protection level

请提出一些方法来在不同的页面上使用一个页面的功能而不会出错。注意:我已经搜索了互联网,但没有网站解决我的问题(包括 StackOverflow)。

4

1 回答 1

1

尝试在 Settings 类中定义“MainPage”属性,以便您可以通过它访问 MainPage。顺便说一句,在这种情况下,ChangeAcrylicOpacity() 的访问修饰符应该是“公共的”或“内部的”。

// Settings.xaml.cs
private MainPage MainPage
{
    get { return (Window.Current.Content as Frame)?.Content as MainPage; }
}
private void Slider_AcrylicValueChanged(object sender, RangeBaseValueChangedEventArgs e)
{
    Slider slider = sender as Slider;
    double tintOpacity = slider.Value;
    MainPage.ChangeAcrylicOpacity(tintOpacity);
}
于 2020-03-15T00:14:20.597 回答