Xamarin - 获取控制维度
Xamarin - get control dimensions
我真的不知道如何在 Xamarin Forms 中获取控件维度。我认为这会很简单:
View control = GetControlSomehow();
var bounds = control.Bounds;
但是 View.Bounds return 取决于控件是否在网格内,情况会有所不同;或者主要布局是 AbsoluteLayout 还是其他布局。
例如,如果我这样做:
<AbsoluteLayout>
<Button AbsoluteLayout.LayoutBounds="200,200" x:Name="btn" Text="Btn"/>
</AbsoluteLayout>
然后我无法读取该按钮的实际高度或宽度。虽然 MSDN 说高度和宽度属性应该 return 这些值。
我什至试过:
var width = ctrl.Width;
或
var width = ctrl.GetValue(WidthProperty);
但实际上这些都不起作用。我总是得到 -1
那么怎样才能得到可靠的控制维度呢?
这对我有用
<Button Text="Click" Clicked="Clicked" />
public void Clicked(object sender, EventArgs a)
{
var btn = (Button)sender;
var w = btn.Width;
var h = btn.Height;
DisplayAlert("Dimesions", $"{w}x{h}","OK");
}
显示如何使用数据绑定传递按钮本身的完整示例。
AbsoluteLayoutPage.xaml:
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="TestBugs.AbsoluteLayoutPage">
<ContentPage.Content>
<AbsoluteLayout>
<Button AbsoluteLayout.LayoutBounds="200,200" x:Name="btn" Text="Btn"
Command="{Binding ButtonCommand}" CommandParameter="{x:Reference btn}"/>
</AbsoluteLayout>
</ContentPage.Content>
</ContentPage>
AbsoluteLayoutPage.xaml.cs:
using System.Diagnostics;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;
namespace TestBugs
{
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class AbsoluteLayoutPage : ContentPage
{
public AbsoluteLayoutPage()
{
ButtonCommand = new Command(ButtonClick);
InitializeComponent();
BindingContext = this;
}
private void ButtonClick(object ob)
{
var button = ob as View;
var bounds = btn.Bounds;
var width = btn.Width;
var height = btn.Height;
Debug.WriteLine($"--- ButtonClick {bounds}, ({width},{height})---");
}
public Command ButtonCommand { get; set; }
}
}
单击按钮时,在 VS 输出窗格中:
[0:] --- ButtonClick {X=200 Y=200 Width=88 Height=48}, (88,48)---
对不起大家,我的错。
我在两页上测试它——真实的一页和测试的一页。测试页面以不同的方式构建,我确认问题是在我检索这些值时及时出现的。尚未绘制控件。
非常抱歉打扰大家,感谢大家为此付出宝贵的时间。
我真的不知道如何在 Xamarin Forms 中获取控件维度。我认为这会很简单:
View control = GetControlSomehow();
var bounds = control.Bounds;
但是 View.Bounds return 取决于控件是否在网格内,情况会有所不同;或者主要布局是 AbsoluteLayout 还是其他布局。
例如,如果我这样做:
<AbsoluteLayout>
<Button AbsoluteLayout.LayoutBounds="200,200" x:Name="btn" Text="Btn"/>
</AbsoluteLayout>
然后我无法读取该按钮的实际高度或宽度。虽然 MSDN 说高度和宽度属性应该 return 这些值。
我什至试过:
var width = ctrl.Width;
或
var width = ctrl.GetValue(WidthProperty);
但实际上这些都不起作用。我总是得到 -1
那么怎样才能得到可靠的控制维度呢?
这对我有用
<Button Text="Click" Clicked="Clicked" />
public void Clicked(object sender, EventArgs a)
{
var btn = (Button)sender;
var w = btn.Width;
var h = btn.Height;
DisplayAlert("Dimesions", $"{w}x{h}","OK");
}
显示如何使用数据绑定传递按钮本身的完整示例。
AbsoluteLayoutPage.xaml:
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="TestBugs.AbsoluteLayoutPage">
<ContentPage.Content>
<AbsoluteLayout>
<Button AbsoluteLayout.LayoutBounds="200,200" x:Name="btn" Text="Btn"
Command="{Binding ButtonCommand}" CommandParameter="{x:Reference btn}"/>
</AbsoluteLayout>
</ContentPage.Content>
</ContentPage>
AbsoluteLayoutPage.xaml.cs:
using System.Diagnostics;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;
namespace TestBugs
{
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class AbsoluteLayoutPage : ContentPage
{
public AbsoluteLayoutPage()
{
ButtonCommand = new Command(ButtonClick);
InitializeComponent();
BindingContext = this;
}
private void ButtonClick(object ob)
{
var button = ob as View;
var bounds = btn.Bounds;
var width = btn.Width;
var height = btn.Height;
Debug.WriteLine($"--- ButtonClick {bounds}, ({width},{height})---");
}
public Command ButtonCommand { get; set; }
}
}
单击按钮时,在 VS 输出窗格中:
[0:] --- ButtonClick {X=200 Y=200 Width=88 Height=48}, (88,48)---
对不起大家,我的错。 我在两页上测试它——真实的一页和测试的一页。测试页面以不同的方式构建,我确认问题是在我检索这些值时及时出现的。尚未绘制控件。
非常抱歉打扰大家,感谢大家为此付出宝贵的时间。