C
C#12mo ago
Mekasu0124

✅ AngelSix YouTube Series, cannot get position to work on popup -- Avalonia

using Avalonia;
using Avalonia.Controls;
using Avalonia.Media;
using System;

namespace LoudnessMeter.Views
{
public partial class MainView : UserControl
{
#region Private Members

private Control mChannelConfigPopup;
private Control mChannelConfigButton;
private Control mMainGrid;

#endregion

public MainView()
{
InitializeComponent();

mChannelConfigButton = this.FindControl<Control>("ChannelConfigurationButton") ?? throw new System.Exception("Cannot Find Channel Configuration Button by name");
mChannelConfigPopup = this.FindControl<Control>("ChannelConfigurationPopup") ?? throw new System.Exception("Cannot Find Channel Popup Button by name");
mMainGrid = this.FindControl<Control>("MainGrid") ?? throw new Exception("Cannot find Main Grid by name");
}

public override void Render(DrawingContext context)
{
base.Render(context);

var position = mChannelConfigButton.TranslatePoint(new Point(), mMainGrid) ??
throw new Exception("Cannot get TranslatePoint from Configuration Button");

mChannelConfigPopup.Margin = new Thickness(position.X, 0, 0, position.Y);
}
}
}
using Avalonia;
using Avalonia.Controls;
using Avalonia.Media;
using System;

namespace LoudnessMeter.Views
{
public partial class MainView : UserControl
{
#region Private Members

private Control mChannelConfigPopup;
private Control mChannelConfigButton;
private Control mMainGrid;

#endregion

public MainView()
{
InitializeComponent();

mChannelConfigButton = this.FindControl<Control>("ChannelConfigurationButton") ?? throw new System.Exception("Cannot Find Channel Configuration Button by name");
mChannelConfigPopup = this.FindControl<Control>("ChannelConfigurationPopup") ?? throw new System.Exception("Cannot Find Channel Popup Button by name");
mMainGrid = this.FindControl<Control>("MainGrid") ?? throw new Exception("Cannot find Main Grid by name");
}

public override void Render(DrawingContext context)
{
base.Render(context);

var position = mChannelConfigButton.TranslatePoint(new Point(), mMainGrid) ??
throw new Exception("Cannot get TranslatePoint from Configuration Button");

mChannelConfigPopup.Margin = new Thickness(position.X, 0, 0, position.Y);
}
}
}
I am doing everything he does, and he gets the button to work for him instantly. I have the same code as him at 12:30 in video #9 in his YouTube series for learning Avalonia building a Loudness Meter. The project builds with no issues, but when I run the program to launch the desktop application, it throws an error on mChannelConfigPopup.Margin = new Thickness(position.X, 0, 0, position.Y); as shown in the screenshot and I cannot figure out why.
1 Reply
Mekasu0124
Mekasu012412mo ago
to get it to work I had to change the function a bit
public override void Render(DrawingContext context)
{
base.Render(context);

Dispatcher.UIThread.InvokeAsync(() =>
{
var position = mChannelConfigButton.TranslatePoint(new Point(), mMainGrid) ?? throw new Exception("Cannot get TranslatePoint from Configuration Button");

mChannelConfigPopup.Margin = new Thickness(
position.X,
0,
0,
position.Y);
});
}
}
public override void Render(DrawingContext context)
{
base.Render(context);

Dispatcher.UIThread.InvokeAsync(() =>
{
var position = mChannelConfigButton.TranslatePoint(new Point(), mMainGrid) ?? throw new Exception("Cannot get TranslatePoint from Configuration Button");

mChannelConfigPopup.Margin = new Thickness(
position.X,
0,
0,
position.Y);
});
}
}
but I still don't understand why I had to do this so early when he hasn't done this in the video yet