小编Buc*_*ter的帖子

绑定到UserControl DependencyProperty

我创建了一个带有一些DependencyProperties的UserControl(在这个例子中只有一个字符串属性).当我实例化Usercontrol时,我可以设置UserControl的属性,并按预期显示.当我尝试通过Binding替换静态文本时,不显示任何内容.

我的UserControl看起来如下:

<User Control x:Class="TestUserControBinding.MyUserControl"
             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" 
             mc:Ignorable="d" 
             d:DesignHeight="30" d:DesignWidth="100">
    <Grid>
    <Label Content="{Binding MyText}"/>
  </Grid>
</UserControl>
Run Code Online (Sandbox Code Playgroud)

守则背后是:

namespace TestUserControBinding {

  public partial class MyUserControl : UserControl {
    public MyUserControl() {
      InitializeComponent();
      this.DataContext = this;
    }

    public static readonly DependencyProperty MyTextProperty = 
                   DependencyProperty.Register(
                         "MyText", 
                          typeof(string), 
                          typeof(MyUserControl));

    public string MyText {
      get {
        return (string)GetValue(MyTextProperty);
      }
      set {
        SetValue(MyTextProperty, value);
      }
    }// MyText

  }
}
Run Code Online (Sandbox Code Playgroud)

当我在MainWindow中尝试这个时,一切都如预期:

<Window x:Class="TestUserControBinding.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:TestUserControBinding"
        Title="MainWindow" Height="350" Width="525">
  <StackPanel>
    <local:MyUserControl MyText="Hello World!"/>
  </StackPanel>
</Window> …
Run Code Online (Sandbox Code Playgroud)

c# wpf binding user-controls dependency-properties

12
推荐指数
2
解决办法
9706
查看次数

C#async/await进度报告不是预期的顺序

我正在尝试使用async/await和progress报告,因此编写了一个异步文件复制方法,在每次复制的MB之后报告进度:

public async Task CopyFileAsync(string sourceFile, string destFile, CancellationToken ct, IProgress<int> progress) {

  var bufferSize = 1024*1024 ;
  byte[] bytes = new byte[bufferSize];
  using(var source = new FileStream(sourceFile, FileMode.Open, FileAccess.Read)){
    using(var dest = new FileStream(destFile, FileMode.Create, FileAccess.Write)){

      var totalBytes = source.Length;
      var copiedBytes = 0;
      var bytesRead = -1;
      while ((bytesRead = await source.ReadAsync(bytes, 0, bufferSize, ct)) > 0)
      {
        await dest.WriteAsync(bytes, 0, bytesRead, ct);
        copiedBytes += bytesRead;
        progress?.Report((int)(copiedBytes * 100 / totalBytes));
      }
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

在控制台应用程序中创建一个随机内容为10MB的I文件,然后使用上面的方法复制它:

private void …
Run Code Online (Sandbox Code Playgroud)

c# asynchronous progress async-await

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

EF Core Update-Database 命令为用户创建架构而不是使用 dbo

我已经DbContext使用 EF Core 2.0创建了一个,并且已经Update-Database在 PM-Console 中使用的开发环境中应用了大量迁移。这些表是dbo按预期使用localDb 上的架构创建的,正如我从 EF6 所知。

但是,当我将该Update-Database命令应用于 MSSQL Server 上的生产数据库时,它会创建一个名为Domain\Username(我的机器上的用户)的架构。当我使用其指定的域帐户运行服务器应用程序时,它无法访问数据库。作为一种解决方法,我与 IT 基础架构人员商量,允许此帐户在我的计算机上执行软件,并通过使用此帐户执行 VisualStudio 来应用迁移。然后为这个特殊帐户创建一个模式,此后它可以从网络服务器应用程序访问数据库。它有效,因为这个特殊帐户是目前唯一需要直接访问数据库的帐户(除了我自己)。但这应该是一个永久的解决方案。

有谁知道解决方案?该数据库的创建方式与之前的 EF6 项目完全相同。为什么每个用户模式只在将迁移应用到生产系统时使用,而不是在我的开发机器上使用,我如何手动控制模式?我试过了,modelBuilder.HasDefaultSchema("dbo");但这不起作用。实际上它似乎根本没有任何影响。

sql-server migration entity-framework entity-framework-core .net-core

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