小编Geo*_*uke的帖子

WPF 自定义控件无法直接获取内容

我无法在自定义控件中放入任何直接内容,请看一下:

    <Style TargetType="local:MyCustomControl">    
         <Setter Property="Template">           
            <Setter.Value>              
                <ControlTemplate TargetType="local:MyCustomControl">
                    <Grid>                  
                        <Viewport3D />                      
                        <!-- the viewport is working (proof provided) -->
                        <!-- both borders are needed -->

                        <Border>
                            <Border>                               
                                <ContentPresenter  />    
                            </Border>
                        </Border>                   
                    </Grid>             
                </ControlTemplate>              
            </Setter.Value>         
        </Setter>    
    </Style>
Run Code Online (Sandbox Code Playgroud)

该类派生自Control,在静态构造函数中DefaultStyleKeyProperty.OverrideMetadata设置。

当我尝试使用 MyCustomControl 时:

    <local:MyCustomControl VerticalAlignment="Stretch" HorizontalAlignment="Stretch">
        <TextBlock Margin="10,0,0,0" FontSize="16" Text="some test value" />
    </local:MyCustomControl>
Run Code Online (Sandbox Code Playgroud)

显示此错误消息:

无法将内容添加到 MyCustomControl MyNamespace.MyCustomControl 类型的对象

可能是什么问题呢?Contentpresenter 有问题吗?

wpf custom-controls contentpresenter

1
推荐指数
1
解决办法
1616
查看次数

C#异步/等待等待不等待

我正在使用.Net 4.7.2和C#7

在一个类中有服务器方法,所有方法都是无效的。其中一些可以异步执行,其他一些则必须顺序执行。所以我为水货定义了一个清单

private void ExecuteSequential()
{
    SomeMethod1();
    SomeMethod2();
    ExecuteParallelAsync();
    SomeMethod3();
    SomeMethod4();
}


private async void ExecuteParallelAsync()
{
    List<Action> methods = new List<Action>()
    {
        MyMethod1,
        MyMethod2,
        MyMethod3
    };


    await Task.Run( () => { Parallel.ForEach( methods , ( currentMethod ) => currentMethod.Invoke() ); } );            
}
Run Code Online (Sandbox Code Playgroud)

SomeMethod3碰巧执行之前,ExecuteParallelAsync应该完全完成。因为不是这种情况,所以我对async和await的使用显然存在问题。

我做错了什么?

提前致谢!

c# async-await

-1
推荐指数
1
解决办法
158
查看次数

标签 统计

async-await ×1

c# ×1

contentpresenter ×1

custom-controls ×1

wpf ×1