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

被刻印的时光 ゝ 提交于 2019-12-08 04:46:50

问题


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:


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();    
        }


来源:https://stackoverflow.com/questions/17633092/changing-color-of-grid-in-c-sharp-windows-phone-8

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!