Updating changes to the MainPage from a ContentView.

huangapple go评论75阅读模式
英文:

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.

&lt;ContentPage xmlns=&quot;http://schemas.microsoft.com/dotnet/2021/maui&quot;
         xmlns:x=&quot;http://schemas.microsoft.com/winfx/2009/xaml&quot;
         xmlns:Controls=&quot;clr-namespace:Kone.Controls&quot;
         x:Class=&quot;Kone.MainPage&quot; x:FieldModifier=&quot;public&quot;&gt;

&lt;AbsoluteLayout x:Name=&quot;mybackscreen&quot; 
                x:FieldModifier=&quot;public&quot; 
                BackgroundColor=&quot;Red&quot; &gt;
    
&lt;Controls:Options x:Name=&quot;myOptions&quot; x:FieldModifier=&quot;public&quot; IsVisible=&quot;true&quot; AbsoluteLayout.LayoutFlags=&quot;PositionProportional&quot; AbsoluteLayout.LayoutBounds=&quot;0.99, 0.02, 270, 375&quot; /&gt;

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:

&lt;ContentView ...
             x:Class=&quot;Options&quot;&gt;
    &lt;VerticalStackLayout&gt;
        &lt;Button Text=&quot;Click&quot;
                x:Name=&quot;btn&quot;
                Clicked=&quot;btn_Clicked&quot;/&gt;
    &lt;/VerticalStackLayout&gt;
&lt;/ContentView&gt;

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:

&lt;ContentPage ...&gt;
    &lt;AbsoluteLayout x:Name=&quot;mybackscreen&quot;
                    BackgroundColor=&quot;red&quot;&gt;
        &lt;Controls:Options x:Name=&quot;myOptions&quot;
                          OptionsChanged=&quot;btn_Clicked&quot;
                          .../&gt;
    &lt;/AbsoluteLayout&gt;
&lt;/ContentPage&gt;

让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:

&lt;ContentView ...
             x:Class=&quot;Options&quot;&gt;
    &lt;VerticalStackLayout&gt;
        &lt;Button Text=&quot;Click&quot;
                x:Name=&quot;btn&quot;
                Clicked=&quot;btn_Clicked&quot;/&gt;
    &lt;/VerticalStackLayout&gt;
&lt;/ContentView&gt;

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:

&lt;ContentPage ...&gt;
    &lt;AbsoluteLayout x:Name=&quot;mybackscreen&quot;
                    BackgroundColor=&quot;red&quot;&gt;
        &lt;Controls:Options x:Name=&quot;myOptions&quot;
                          OptionsChanged=&quot;btn_Clicked&quot;
                          .../&gt;
    &lt;/AbsoluteLayout&gt;
&lt;/ContentPage&gt;

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.

huangapple
  • 本文由 发表于 2023年5月25日 05:13:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/76327436.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定