[英]Object's ActualWidth and ActualHeight reporting as 0?
我有一個應用程序,其中包含一個Grid
幾個實例,它繼承自一個擴展UserControl
的Abstract類,如下所示:
<abstract:ScoringGrid x:Class="ReadProject.Repeater.ScoringGrid"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:abstract="clr-namespace:ReadProject.AbstractRepeater"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<Grid x:Name="ScoringGridInstance" Width="Auto" Height="Auto">
....
和他們繼承的類(這是一個非常基本的類,只有幾個方法和屬性):
public abstract partial class ScoringGrid : UserControl, INotifyPropertyChanged
{...
這些網格在運行時動態構建,每個網格的大小可能不同。 當用戶關閉應用程序時,我想捕獲每個Grid
的屏幕截圖並將其保存到本地計算機。 為此,我創建了一個方法來獲取對象大小並使用RenderTargetBitmap()
將其保存到文件中。 其中很多都保存沒有問題,但是有些網格返回的ActualWidth
和ActualHeight
為0
,因此無法保存。 我不確定從哪里開始調試這個問題,因為它們都是同一個對象的實例; 怎么可能有些人設置了ActualWidth
和ActualHeight
而其他人沒有? 它們都在應用程序中顯示沒有問題。
如果您需要更多信息/代碼,請與我們聯系。 謝謝!
為了完整起見,我能夠通過在捕獲每個UserControl
的屏幕截圖之前添加這些調用來解決問題:
scoreGrid.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));
scoreGrid.Arrange(new Rect(0, 0, scoreGrid.ScoreGridInstance.DesiredSize.Width, scoreGrid.ScoreGridInstance.DesiredSize.Height));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.