当我尝试打印用户控件时,当我使用异步方法获取数据时,datagrid不显示记录 [英] When I try to print user control, datagrid doesn't show records when I use async method to get the data

查看:91
本文介绍了当我尝试打印用户控件时,当我使用异步方法获取数据时,datagrid不显示记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个具有数据网格的用户控件。我想打印这个用户控件。我知道如何打印此用户控件,并显示datagrid的行,当我使用同步方法从数据库获取数据时,使用实体核心2.0。但是,如果我使用EF的async
方法来获取数据,则不会显示它们的行。

I have a user control that has a datagrid. I want to print this user control. I know how to print this user control, and show the rows of the datagrid, when I use a sync method to get the data from database, using entity core 2.0. However, if I use the async method of EF to get the data, the rows it aren't shown.

我正在使用MVVM模式,但我尝试过使用代码背后,结果是一样的。所以问题我想在构造函数中使用异步方法。

I am using the MVVM patter, but I have tried using code behind and the result is the same. So the problem I guess it is to use async methods in the constructor.

我的代码是这样的:

public MyViewModel() { Task.Run(() => getDataAsync().ConfigureAwait(false));

// getData();
}


private ObservableCollection< MyType> _myItems = new ObservableCollection< MyType>();
public ObservableCollection< MyType> MyItems
{
get {return _myItems; }
设置
{
_myItems = value;
base.RaisePropertyChangedEvent(" MyItems");
}
}


私有异步任务getDataAsync()
{
using(ContextEfCore myDbContext = new ContextEfCore(_optionsDbContext))
{
MyItems = new ObservableCollection< MyType>(等待myDbCOntext.MyType.ToListAsync());
}
}


private void getData()
{
using(ContextEfCore myDbContext = new ContextEfCore(_optionsDbContext))
{
MyItems = new ObservableCollection< MyType>(等待myDbCOntext.MyType.ToList());
}
}

//getData(); } private ObservableCollection<MyType> _myItems = new ObservableCollection<MyType>(); public ObservableCollection<MyType> MyItems { get { return _myItems; } set { _myItems = value; base.RaisePropertyChangedEvent("MyItems"); } } private async Task getDataAsync() { using(ContextEfCore myDbContext = new ContextEfCore(_optionsDbContext)) { MyItems = new ObservableCollection<MyType>(await myDbCOntext.MyType.ToListAsync()); } } private void getData() { using(ContextEfCore myDbContext = new ContextEfCore(_optionsDbContext)) { MyItems = new ObservableCollection<MyType>(await myDbCOntext.MyType.ToList()); } }

当我使用getDataAsync()时不起作用,但是当我使用getData()时它可以工作。

When I use getDataAsync() does't work but when I use getData() it works.

我如何使用asyn方法使其有效?我想避免使用该方法的两个版本,async和sync。

How could I use the asyn method to make it works? I would like to avoid to have the two versión of the method, async and sync.

谢谢。

编辑:我将添加我正在使用的完整代码。

I will added the complete code that I am using.

我要打印的视图的视图模型

My view model of the view that I want to print

{
	public MyViewModelToPrint()
	{
		Task.Run(() => getDataAsyncAsync());
		
		//getData(); //this works
	}




	private ObservableCollection<MyType> _myItems = new ObservableCollection<MyType>();
	public ObservableCollection<LineasFacturas> MyItems
	{
		get { return _myItems; }
		set
		{
			_myItems = value;
			base.RaisePropertyChangedEvent("MyItems");
		}
	}





	private async Task getDataAsync()
	{
		MyItems = new ObservableCollection<MyType>((await Repository.getDataAsync()));
	}



	private void getData()
	{
		MyItems = new ObservableCollection<MyType>((await Repository.getData()));

	}
}

我要打印的视图:

<UserControl x:Class="GTS.CMMS.Client.Views.docFacturasView"
             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" 
             Height="29.9cm" Width="21.0cm">
    <Grid>
            <DataGrid VerticalAlignment="Stretch" HorizontalAlignment="Stretch" Margin="5,5,5,5" Grid.Column="0" Grid.Row="0"
                      ItemsSource="{Binding MyItems}">
            </DataGrid>
        </Grid>
</UserControl>

尝试打印视图的主视图模型:

The main view model that tries to print the view:

public void printDocument()
{
	//Just print to PDF with the default printer of Windows 10 without prompting the selection of the printer
	System.Drawing.Printing.PrintDocument pd = new System.Drawing.Printing.PrintDocument();
	pd.PrinterSettings.PrintFileName = @"d:\dummy-02.pdf";
	pd.PrinterSettings.PrinterName = "Microsoft Print to PDF";
	pd.PrinterSettings.PrintToFile = true;
	pd.PrintPage += PrintPage;
	pd.Print();
}






private void PrintPage(object o, System.Drawing.Printing.PrintPageEventArgs e)
{
	MyViewToPrint myViewToPrint = new MyViewToPrint();
	MyViewModelToPrint myViewModelToPrint = new MyViewModelToPrint();
	myViewToPrint.DataContext = myViewModelToPrint;


	int myFactor;
	myFactor = 6;

	
	myViewToPrint.UpdateLayout();


	System.Windows.Media.Imaging.RenderTargetBitmap rtb = new System.Windows.Media.Imaging
	.RenderTargetBitmap(794 * myFactor, 1122 * myFactor, 96 * miFactor, 96 * myFactor, System.Windows.Media.PixelFormats.Pbgra32);
	rtb.Render(myViewToPrint);


	//convert System.Drawing.Image to WPF image
	System.Windows.Media.Imaging.PngBitmapEncoder encoder = new System.Windows.Media.Imaging.PngBitmapEncoder();
	System.IO.MemoryStream ms = new System.IO.MemoryStream();
	encoder.Frames.Add(System.Windows.Media.Imaging.BitmapFrame.Create(rtb));
	encoder.Save(ms);
	System.Drawing.Image img = System.Drawing.Image.FromStream(ms);
	e.Graphics.DrawImage(img, new System.Drawing.PointF());
}

这就是全部。

推荐答案

由于您尚未展示如何初始化打印件,我唯一能想到的是asycn get未完成。 如果您不使用异步,则操作将完成,然后您可以执行任何其他处理,例如尝试打印。
Since you haven't shown how you are initializing the print the only thing I can think of is that the asycn get is not completed.  If you don't use async the operation will finish before you can do any other processing like attempting to print.


这篇关于当我尝试打印用户控件时,当我使用异步方法获取数据时,datagrid不显示记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆