longlistselector中的Width =“*”给出错误“错误HRESULT E_FAIL已从调用COM组件返回”

时间:2013-10-31 18:27:36

标签: c# xaml visual-studio-2012 windows-phone-8

我使用了this page中所示的DataTemplateSelector示例,并对其进行了一些修改,以满足我的要求,如下所示。

public abstract class TemplateSelector : ContentControl
{
    public abstract DataTemplate SelectTemplate(object item, DependencyObject container);

    protected override void OnContentChanged(object oldContent, object newContent)
    {
        base.OnContentChanged(oldContent, newContent);

        var parent = GetParentByType<LongListSelector>(this);

        ContentTemplate = SelectTemplate(newContent, this);
    }

    private static T GetParentByType<T>(DependencyObject element) where T : FrameworkElement
    {
        T result = null;
        DependencyObject parent = VisualTreeHelper.GetParent(element);

        while (parent != null)
        {
            result = parent as T;

            if (result != null)
            {
                return result;
            }

            parent = VisualTreeHelper.GetParent(parent);
        }

        return null;
    }
}


public class MyTemplateSelector : TemplateSelector
{
    public DataTemplate one { get; set; }
    public DataTemplate two { get; set; }
    public DataTemplate three { get; set; }
    public DataTemplate four { get; set; }
    public DataTemplate five { get; set; }
    public DataTemplate six { get; set; }
    public DataTemplate seven { get; set; }
    public DataTemplate eight { get; set; }
    public DataTemplate nine { get; set; }

    public override DataTemplate SelectTemplate(object item, DependencyObject container)
    {
        PoiData poiItem = item as PoiData;

        if (poiItem != null)
        {
            switch (poiItem.Type)
            {
                case "atm":
                    return one;

                case "food":
                    return two;

                case "hospital":
                    return three;

                case "police":
                    return four;

                case "pharmacy":
                    return five;

                case "gas_station":
                    return six;

                case "hindu_temple":
                    return seven;

                case "train_station":
                    return eight;

                case "movie_theater":
                    return nine;

            }
        }
        return null;
    }
}    

我的xaml看起来像这样。

<phone:PhoneApplicationPage.Resources>

<DataTemplate x:Key="one">
        <StackPanel Height="75">
            <Grid Height="65"
                  Background="Red"
                  HorizontalAlignment="Left"          
                  Margin="0, 0, 0, 12">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="65" />
                    <ColumnDefinition Width="auto" />
                </Grid.ColumnDefinitions>
                <Grid VerticalAlignment="Top"
                                  HorizontalAlignment="Left"
                                  Width="65"
                                  Height="65"
                                  Background="White"
                                  Grid.Column="0"
                                  Margin="0, 0, 0, 0">
                </Grid>
                <Grid Grid.Column="1" Width="auto">
                    <TextBlock Text="{Binding Title}" 
                               FontSize="30" Margin="10,0,0,0" 
                               VerticalAlignment="Center" 
                               Foreground="White"/>
                </Grid>

            </Grid>
        </StackPanel>
</DataTemplate>
.
.
.
</phone:PhoneApplicationPage.Resources>

<DataTemplate x:Key="SelectingTemplate">
            <local:MyTemplateSelector Content="{Binding}"
                                      one="{StaticResource one}"
                                      two="{StaticResource two}"
                                      three="{StaticResource three}"
                                      four="{StaticResource four}"
                                      five="{StaticResource five}"
                                      six="{StaticResource six}"
                                      seven="{StaticResource seven}"
                                      eight="{StaticResource eight}"
                                      nine="{StaticResource nine}" />
</DataTemplate>

这是全景项目xaml

<phone:PanoramaItem Header="{Binding Path=LocalizedResources.AroundMe, Source={StaticResource LocalizedStrings}}">
                <!--Double wide Panorama with large image placeholders-->
                <phone:LongListSelector Margin="12,-20,0,75"
                                        ItemsSource="{Binding Poi.Items}"
                                        ItemTemplate="{StaticResource SelectingTemplate}"
                                        SelectionChanged="PoiSelectionChanged" >
                </phone:LongListSelector>
            </phone:PanoramaItem>

我的问题是,如果我使用此代码,stackpanel始终将自己与panorama项的中心对齐,无论我将HorizontalAlignment设置为left还是rightstretch

如果我将width或子stackpanel的{​​{1}}属性设置为grid以占用整个屏幕空间,我会收到错误认为是完全无关紧要的。

*

我应该做些什么来设置{MS.Internal.WrappedException: Error HRESULT E_FAIL has been returned from a call to a COM component. ---> System.Exception: Error HRESULT E_FAIL has been returned from a call to a COM component. at MS.Internal.XcpImports.CheckHResult(UInt32 hr) at MS.Internal.XcpImports.FrameworkElement_MeasureOverride(FrameworkElement element, Size availableSize) at System.Windows.FrameworkElement.MeasureOverride(Size availableSize) at System.Windows.FrameworkElement.MeasureOverride(IntPtr nativeTarget, Double inWidth, Double inHeight, Double& outWidth, Double& outHeight) --- End of inner exception stack trace ---} 的宽度以占据整个屏幕宽度?有没有解决方法?

当我将stackpanel设置为width时,为什么会抛出此异常。

2 个答案:

答案 0 :(得分:2)

解决问题的两件事

  1. 删除父堆栈面板,仅将Grid作为DataTemplate的内容
  2. 将第二列ColumnDefinition的宽度设置为'*'
  3. 为什么这样做?

    1. StackPanels可以占用尽可能少的空间。因此,将StackPanel作为“父元素”或DataTemplate的内容会使您的内容“缩小”到与告知一样多的空间(通过设置显式宽度)。
    2. 网格的第二列被告知“自动”大小。这意味着只占用需要渲染的空间(就像StackPanel正在做的那样!)。将大小设置为“*”表示列占用的空间与可用一样多。

答案 1 :(得分:1)

我找到了解决方案!我在这里找到了谷歌搜索:http://webcache.googleusercontent.com/search?q=cache:KDZZtrw9eG0J:y2bd.me/blog/2013/08/16/fixing-alignment-issues-with-datatemplateselector/+&cd=6&hl=en&ct=clnk&gl=us

基本上,引用TemplateSelector的XAML标记需要将Horizo​​ntalContentAlignment设置为stretch。像这样:

<DataTemplate x:Key="YourTemplateSelectorKey">
    <namespace:YourTemplateSelectorClass HorizontalContentAlignment="Stretch" ... />
</DataTemplate>

多田!