RJ90 0 Posted March 31, 2011 Report Share Posted March 31, 2011 Boas! Estou a fazer através de C# uma alteração do fundo de um botão. private void button1_Click(object sender, RoutedEventArgs e) { ImageBrush img1 = new ImageBrush(); img1.ImageSource = new BitmapImage(new Uri("/BTN;component/Images/btnred.png", UriKind.Relative)); button1.Background = img1; } O problema está no caminho das imagens. As imagens ficam em WPF numa pasta Images (que pertence ao projecto): Como faço para aceder as imagens que estão nessa pasta? Cumprimentos, ◣ Samsung 700Z5A-S01PT ◥ ◣ Samsung Ominia W ◥ ᥡ What Else? ᥡ Link to post Share on other sites
saramgsilva 1 Posted March 31, 2011 Report Share Posted March 31, 2011 O que eu normalmente faço é: num dicionário de resource, em xaml, crio um imagesource em que digo o source, que é o caminho para a imagem. <ImageSource x:Uid="Image_Loading" x:Key="Image_Loading" >/MyProject;component/Resources/Images/loading.gif</ImageSource> e depois em C#, considerando que adicionei esse dicionário de resource no app.xaml, faço algo tipo ImageBrush img1 = new ImageBrush(); img1.ImageSource = (ImageSource) Application.Current.Resources["Image_Loading"]; button1.Background = img1; Tenta lá. www.saramgsilva.com As minhas apps no WP7 Marketplace Youtube : Galinho - Windows Phone 7.5 Link to post Share on other sites
RJ90 0 Posted March 31, 2011 Author Report Share Posted March 31, 2011 Consegui resolver. Fiz assim: private void button1_Click(object sender, RoutedEventArgs e) { ImageBrush img1 = new ImageBrush(); img1.ImageSource = new BitmapImage(new Uri("pack://application:,,,/BTN;component/Images/btnred.png", UriKind.RelativeOrAbsolute)); button1.Background = img1; } Qual achas o mais correcto saramgsilva a que dizes ou esta que encontrei? Com o teu código: Num Styles.xaml (ResourceDictionary) fiz isto: <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> <ImageSource x:Uid="btnRed" x:Key="btnRed" >/BTN;component/Images/btnred.png</ImageSource> </ResourceDictionary> Na App.xaml fiz o seguinte: <Application.Resources> <ResourceDictionary Source="Styles.xaml"/> </Application.Resources> Em C#: ImageBrush img1 = new ImageBrush(); img1.ImageSource = (ImageSource)Application.Current.Resources["btnRed"]; button1.Background = img1; Obrigado! ◣ Samsung 700Z5A-S01PT ◥ ◣ Samsung Ominia W ◥ ᥡ What Else? ᥡ Link to post Share on other sites
saramgsilva 1 Posted March 31, 2011 Report Share Posted March 31, 2011 Consegui resolver. Fiz assim: private void button1_Click(object sender, RoutedEventArgs e) { ImageBrush img1 = new ImageBrush(); img1.ImageSource = new BitmapImage(new Uri("pack://application:,,,/BTN;component/Images/btnred.png", UriKind.RelativeOrAbsolute)); button1.Background = img1; } Qual achas o mais correcto saramgsilva a que dizes ou esta que encontrei? Eu prefiro o meu caso, porque defino o imagesource uma unica vez e uso quantas vezes quero na aplicação. Quando pretendo fazer uma alteração à imagem, só tenho que me preocupar com um único sitio www.saramgsilva.com As minhas apps no WP7 Marketplace Youtube : Galinho - Windows Phone 7.5 Link to post Share on other sites
RJ90 0 Posted March 31, 2011 Author Report Share Posted March 31, 2011 Obrigado saramgsilva! ◣ Samsung 700Z5A-S01PT ◥ ◣ Samsung Ominia W ◥ ᥡ What Else? ᥡ Link to post Share on other sites
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now