好的,这让我感到困惑,应该很简单
我有这段代码
private void Expander_Expanded(object sender, RoutedEventArgs e)
{
var expand = e.OriginalSource as Expander;
if (expand != null)
{
var layer = sender as LayerBase;
var middle = expand.TranslatePoint(new Point(), layer) + new Vector(expand.ActualWidth/2,expand.ActualHeight/2);
Location.Centre = layer.ScreenToGeoPoint(middle);
e.Handled = true;
}
}
这就是代码应该做的事情,
这是为了确保扩展器位于显示的地图的中心,而不是相对于地图移动扩展器(即左上角仍然在正确的地理位置)
代码本身正在运行,但是,ActualHeight和ActualWidth正在返回折叠扩展器的大小,这会大幅度地抛弃中心点,我假设这是因为Expanded事件在控件重绘之前触发。那么如何在可视树重绘后捕获扩展已更改?
答案 0 :(得分:0)
private async void Expander_Expanded(object sender, RoutedEventArgs e)
{
var expand = e.OriginalSource as Expander;
if (expand != null)
{
var layer = sender as LayerBase;
// magic
await Dispatcher.InvokeAsync( ( ) => { } );
expand.UpdateLayout();
var middle = expand.TranslatePoint(new Point(), layer) + new Vector(expand.ActualWidth/2,expand.ActualHeight/2);
Location.Centre = layer.ScreenToGeoPoint(middle);
e.Handled = true;
}
}
这有效:)