英文:
Updating changes to the the MainPage from a ContentView
问题
以下是您要翻译的内容:
我对 .net MAUI 还不熟悉。这是我的 MainPage,其中有一个名为 mybackscreen 的 AbsoluteLayout。
我创建了一个名为 myOptions 的 ContentView 用户控件,并将其添加到 MainPage 中。
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:Controls="clr-namespace:Kone.Controls"
x:Class="Kone.MainPage" x:FieldModifier="public">
<AbsoluteLayout x:Name="mybackscreen"
x:FieldModifier="public"
BackgroundColor="Red" >
<Controls:Options x:Name="myOptions" x:FieldModifier="public" IsVisible="true" AbsoluteLayout.LayoutFlags="PositionProportional" AbsoluteLayout.LayoutBounds="0.99, 0.02, 270, 375" />
</ContentPage>
在 ContentView myOptions 上有一个按钮,当点击时应该更改 "mybackscreen" 的背景颜色,但它没有改变。我知道这与设置 MainPage 的一个实例有关。
MainPage newinstance = new MainPage();
newinstance.mybackscreen.BackgroundColor = Colors.Black;
我必须漏掉了一些用于更新或刷新 UI 视图的代码,因为什么都没有改变。这是我在 WinForm 应用程序中使用的代码,但无法直接转换,因此我只是用上面的代码进行了简化。
public static MainPage Default
{
get
{
if (defaultInstance == null)
{
defaultInstance = new MainPage();
defaultInstance.FormClosed += new FormClosedEventHandler(defaultInstance_FormClosed);
}
return defaultInstance;
}
set
{
defaultInstance = value;
}
}
static void defaultInstance_FormClosed(object sender, FormClosedEventArgs e)
{
defaultInstance = null;
}
英文:
I am new to .net MAUI. Here is my MainPage with and Absolutelayout named mybackscreen.
I created a ContentView User Control named myOptions and added it to the MainPage.
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:Controls="clr-namespace:Kone.Controls"
x:Class="Kone.MainPage" x:FieldModifier="public">
<AbsoluteLayout x:Name="mybackscreen"
x:FieldModifier="public"
BackgroundColor="Red" >
<Controls:Options x:Name="myOptions" x:FieldModifier="public" IsVisible="true" AbsoluteLayout.LayoutFlags="PositionProportional" AbsoluteLayout.LayoutBounds="0.99, 0.02, 270, 375" />
There is a button on the ContentView myOptions when clicked should change the backgroundcolor of "mybackscreen" but it doesnt change. I know this has to do with setting an instance of the MainPage.
MainPage newinstance = new MainPage();
newinstance.mybackscreen.BackgroundColor = Colors.Black;
I must be missing some code to update or refresh the UI View because nothing changes. This is what i used in my WinForm App but wasnt able to convert so i just simplified it with the above code:
public static MainPage Default
{
get
{
if (defaultInstance == null)
{
defaultInstance = new MainPage();
defaultInstance.FormClosed += new FormClosedEventHandler(defaultInstance_FormClosed);
}
return defaultInstance;
}
set
{
defaultInstance = value;
}
}
static void defaultInstance_FormClosed(object sender, FormClosedEventArgs e)
{
defaultInstance = null;
}
答案1
得分: 0
以下是您要翻译的内容:
如Jason所说,您可以创建一个事件来实现它:
Options.xaml:
<ContentView ...
x:Class="Options">
<VerticalStackLayout>
<Button Text="Click"
x:Name="btn"
Clicked="btn_Clicked"/>
</VerticalStackLayout>
</ContentView>
Options.xaml.cs:
public partial class Options : ContentView
{
public event EventHandler OptionsChanged;
public Options()
{
InitializeComponent();
}
private void btn_Clicked(object sender, EventArgs e)
{
OptionsChanged.Invoke(this, EventArgs.Empty);
}
}
创建事件“OptionsChanged”,并在方法“btn_Clicked”中调用它。然后返回到MainPage.xaml:
<ContentPage ...>
<AbsoluteLayout x:Name="mybackscreen"
BackgroundColor="red">
<Controls:Options x:Name="myOptions"
OptionsChanged="btn_Clicked"
.../>
</AbsoluteLayout>
</ContentPage>
让MainPage的btn_Clicked方法订阅Options的OptionsChanged事件。以下是MainPage.xaml.cs:
public partial class MainPage : ContentPage
{
public MainPage()
{
InitializeComponent();
}
private void btn_Clicked(object sender, EventArgs e)
{
mybackscreen.BackgroundColor = Colors.Black;
}
}
英文:
As Jason said, you can create an event to achieve it:
Options.xaml:
<ContentView ...
x:Class="Options">
<VerticalStackLayout>
<Button Text="Click"
x:Name="btn"
Clicked="btn_Clicked"/>
</VerticalStackLayout>
</ContentView>
Options.xaml.cs:
public partial class Options : ContentView
{
public event EventHandler OptionsChanged;
public Options()
{
InitializeComponent();
}
private void btn_Clicked(object sender, EventArgs e)
{
OptionsChanged.Invoke(this, EventArgs.Empty);
}
}
Create event OptionsChanged
and call it in method btn_Clicked
. Then back to MainPage.xaml:
<ContentPage ...>
<AbsoluteLayout x:Name="mybackscreen"
BackgroundColor="red">
<Controls:Options x:Name="myOptions"
OptionsChanged="btn_Clicked"
.../>
</AbsoluteLayout>
</ContentPage>
Let method btn_Clicked
of MainPage subscribe to OptionsChanged
of Options. And here is MainPage.xaml.cs:
public partial class MainPage : ContentPage
{
public MainPage()
{
InitializeComponent();
}
private void btn_Clicked(object sender, EventArgs e)
{
mybackscreen.BackgroundColor = Colors.Black;
}
}
答案2
得分: 0
我发现这个方法目前没有任何不利影响:
public partial class MainPage : ContentPage
{
public static MainPage Default;
public MainPage()
{
InitializeComponent();
Default = this;
}
public static void ColorChange(FieldInfo colorField)// Back color of back screen
{
Default.mybackscreen.BackgroundColor = (Color)(colorField.GetValue(null));
}
}
从另一个类调用静态方法允许主 UI 使用 Default 进行更改。
英文:
I found this works without any downside so far:
public partial class MainPage : ContentPage
{
public static MainPage Default;
public MainPage()
{
InitializeComponent();
Default = this;
}
public static void ColorChange(FieldInfo colorField)// Back color of back screen
{
Default.mybackscreen.BackgroundColor = (Color)(colorField.GetValue(null));
}
Calling a static method from another class allows the main ui to change by using Default.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论