Changing color of grid in C# (Windows Phone 8)

2019-06-07 00:11发布

I am trying to change background color of grid (with name "colorPlace") in my simple app (when I click on button). I tried (where red, green and blue are bytes):

colorPlace.Background = new SolidColorBrush(Color.FromArgb(255, red, green, blue));

and:

colorPlace.SetValue(BackgroundProperty, "#FFFFFFFF");

And all I just get is:

An exception of type 'System.NullReferenceException' occurred in Project.DLL but was not handled in user code

Any ideas how to do it?


EDIT: Oh, I just find an solution - all changes in elements (grid, buttons etc.) should be done AFTER InitializeComponent(); line.

1条回答
老娘就宠你
2楼-- · 2019-06-07 00:53

Everything looks good. Something wrong with the project itself.. it looks like that the colorPlace is null at the runtime. or you are trying to set the background before the InitializeComponent call.

public MainPage()
        {
            colorPlace.Background = new SolidColorBrush(Color.FromArgb(255, 100, 100, 100));
            InitializeComponent();    
        }
查看更多
登录 后发表回答