1

I have an interface:

interface IFoo<out T> 
{
   T Get();
}

and some instances like IFoo<int> a, IFoo<User> u, IFoo<string> s and etc. There is a List<IFoo<object>> used to collect them. But variance doesn't work for value types, is there a proper way to put them in the list?


Issue while saving a Transparent Canvas in WPF

I have created a Grid in WPF with Red background. The Grid contains a transparent Canvas with some fixed size. Now while trying to export the Canvas as Image, I am getting an image with black background. But when there is some color in Canvas (say White or Red), I am getting a proper image. Can anybody please tell me why the image is generating with black background if a Canvas has a transparent color.

Example:

Grid grid = new Grid();
            grid.Background = new SolidColorBrush(Colors.Red);
            grid.Width = 500;
            grid.Height = 300;

            Canvas c = new Canvas();
            c.Width = 500;
            c.Height = 300;
            c.Background = new SolidColorBrush(Colors.Transparent);
            c.MouseLeftButtonUp += new MouseButtonEventHandler(c_MouseLeftButtonUp);

            grid.Children.Add(c);

            LayoutRoot.Children.Add(grid);

Inside MouseEvent handler of Canvas, I am saving it as jpg image.

4

1 に答える 1

3

このリストにはジェネリックが必要ないように見えるため、インターフェイスに非ジェネリック インターフェイスを実装させることができます。

interface IFoo<out T> : IFoo { }

そうすれば、すべてのオブジェクトが同じインターフェースを実装します。これらには共通点があるため、これは悪い考えではないかもしれません。これで、単純にList<IFoo>.

于 2011-10-10T05:38:46.553 に答える