i have tried to make a wpf application with blinking icon with below steps:
set Icon1.ico as the application icon in project properties
create a DispatcherTimer
add a function to DispatcherTimer
in the function check icon name and change it
the code is like this
private void ToggleIconVisibility(object sender, EventArgs e)
{
if (((BitmapImage)Icon).UriSource.OriginalString.CompareTo("Icon1.ico") == 0)
{
Icon = new BitmapImage(new Uri("Icon2.ico", UriKind.RelativeOrAbsolute));
}
else
{
Icon = new BitmapImage(new Uri("Icon1.ico", UriKind.RelativeOrAbsolute));
}
}
it works well when i run it in visual studio, but after publishing application could not find Icon2 and crashed. both icon files are located in the root folder of project.
I would be appreciate if someone could help me.
I don't know what you're expecting icon to be or what other code you have.
When I just set the icon in the app that didn't work for me with your code.
I therefore added an int to flag which icon is expected to be current.
I set my two icon files as content, copy if newer.
Which means they're copied alongside the exe.
This code then works for me:
private int currentIcon = 1;
private void Button_Click(object sender, RoutedEventArgs e)
{
if (currentIcon ==1 )
{
Icon = new BitmapImage(new Uri("Icon2.ico", UriKind.RelativeOrAbsolute));
currentIcon = 2;
return;
}
Icon = new BitmapImage(new Uri("Icon1.ico", UriKind.RelativeOrAbsolute));
currentIcon = 1;
}
Related
I have to define a property grid only in code behind
Xceed.Wpf.Toolkit.PropertyGrid.PropertyGrid m_PropertyGrid;
Then I want to be able to customize it according to the selected object.
So I define the event
private void M_PropertyGrid_SelectedObjectChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
{
foreach (Xceed.Wpf.Toolkit.PropertyGrid.PropertyItem item in (sender as Xceed.Wpf.Toolkit.PropertyGrid.PropertyGrid).Properties)
{
item.Background = new SolidColorBrush(Colors.Green);
item.Foreground = new SolidColorBrush(Colors.Red);
item.BorderBrush = new SolidColorBrush(Colors.Orange);
item.BorderThickness = new Thickness(3);
}
}
but the effect is the following
it seems that the effect is only on the first column and not the second one.
what's more I can't change the effect on enums so when a combo opens the effect is unreadable
thank you for any help
On button click I want to change the canvas image(background image) to another image which is in a folder(images is the folder name). This is what I have tried:
private void Button_Click_2(object sender, RoutedEventArgs e)
{
drawingCanvas.Children.Clear();
ImageBrush imageBrush = new ImageBrush();
imageBrush.ImageSource = new BitmapImage((new Uri(#"../Images/canvas.png", UriKind.Relative)));
drawingCanvas.Background = imageBrush;
}
First I'm clearing the contents of the canvas, deleting the previous image. Now want to set the background to another image. error: URI not handled exception. Any solutions will be appreciated. Thanks.
An image resource is loaded by a Resource File Pack URI:
imageBrush.ImageSource = new BitmapImage(
new Uri("pack://application:,,,/Images/canvas.png"));
In addition, the Build Actionof the image file has to be set to Resource, as shown in this answer.
I have three windows. FirstWindow, SecondWindow and ThirdWindow. FirstWindow has button and click on this button opens the SecondWindow. Analogously, SecondWindow has button and click on this button opens the ThirdWindow. Owner property of the SecondWindow is set as FirstWindow and Owner property of the ThirdWindow is set as SecondWindow. The scenario discribing problem:
Open all windows in a row. It will be looked like this:
Then minimize all windows by click on corresponding icon at top right corner of ThirdWindow.
If you will try to maximize all windows by clicking on FirstLevelWindow or ThirdLevelWinow in taskbar - all will be ok, three windows will be maximized. But if you will click on SecondWindow you will see this:
How can I fix it, or it is just WPF bug? I can give archived expample project if it helps.
UPDATE
Minimize window - click "_" icon, left icon in iconbar of the window. All windows are modal, i.e it opens with ShowDialog() method, not with Show() method. So if you minimize third window - all the windows will be minimized.
Here the code if you don't want download project by link:
FirstWindow XAML:
<Button Click="OpenChildWindow"
Content="ChildWindow"/>
FirstWindow .cs:
private void OpenChildWindow(Object sender, RoutedEventArgs e)
{
var window = new SecondLevelWindow();
window.Owner = this;
window.ShowDialog();
}
SecondWindow XAML:
<Button Click="OpenChildWindow"
Content="ChildWindow"/>
SecondWindow .cs:
private void OpenChildWindow(Object sender, RoutedEventArgs e)
{
var window = new ThirdLevelWindow();
window.Owner = this;
window.ShowDialog();
}
ThirdWindow is empty window without any content.
Here link to example project
I've just found, that bug is not reproduced if property ResizeMode of ThirdWindow is set to "NoResize". Mb it will be usefull information.
Well, I admit I have no idea what is going on. Did you try to add a fourth window? This become even stranger: the second window bring back the third, but the fourth is still not back.
Anyway, If I had to manage this problem, I would keep a reference of my childWindow in each parent Window. This way on any interesting event (like activate on the second window in your example) I could manage the state of my child as required (WindowState.Normal in your case).
It could be something like that: in xaml of secondWindow:
Activated="SecondLevelWindow_OnActivated"
And then in code behind:
private ThirdLevelWindow _window;
public SecondLevelWindow()
{
InitializeComponent();
}
private void OpenChildWindow(Object sender, RoutedEventArgs e)
{
_window = new ThirdLevelWindow ();
_window.Owner = this;
_window.ShowDialog();
}
public void SecondLevelWindow_OnActivated(object sender, EventArgs e)
{
if (_window != null)
{
_window.WindowState = WindowState.Normal;
}
}
This is a start, but you could also inspect your current state to define the state of your child.
Hope it helps.
I need to have an image in my silverlight library and load it into a bitmap. I want to just refer to it like a resource, but not sure how to go about it. I don't have any xaml at all in this library, but what I read seems to indicate I need to do it with xaml.
Here is how I did it in a sample solution using the imageLoaded event. (you know how silverlight just loves async stuff!) The image properties is set to resource/copy always.
public partial class MainPage : UserControl
{
WriteableBitmap myIcon = new WriteableBitmap(100, 100);
public MainPage()
{
InitializeComponent();
LoadImages();
}
public void LoadImages()
{
BitmapImage bmi = new BitmapImage();
bmi.ImageOpened += ImagesLoaded;
bmi.CreateOptions = BitmapCreateOptions.None;
bmi.UriSource = new Uri(App.Current.Host.Source, "/ClientBin/HouseLogo.png");
}
public void ImagesLoaded(object sender, RoutedEventArgs e)
{
BitmapImage bm = (BitmapImage)sender;
myIcon = new WriteableBitmap(bm);
}
private void btnPdf_Click(object sender, RoutedEventArgs e)
{
PDFdoc doc = new PDFdoc(32.0, 32.0, myIcon );
}
}
First of all you say this is a silverlight library hence "Content" images are not useful, you will need to specify the "Resource" build action on your images in this library project. Hence the Url you need to access the image resource is something like "/YourLibraryNameDllName;component/Images/HouseLogo.png". Where you have a folder in your project called "Images" where you are placing these pngs you want to load from your dll.
With that in place you can load the png into a WriteableBitmap without the async pattern using this chunk of code.
StreamResourceInfo sri = Application.GetResourceStream(new Uri("/YourLibraryNameDllName;component/Images/HouseLogo.png", UriKind.Relative));
BitmapSource source = new BitmapImage();
source.SetSource(sri.Stream);
WriteableBitmap myIcon = new WriteableBitmap(source);
I have a textbox where user will enter the url of the image :
suppose the user enters the following string -> C:\Users\malcolm\Desktop\img.png
imgSilverPart is a image control AND imageUrl is a string what i am getting from a textbox.
imgSilverPart.Source = new BitmapImage(new Uri(imageUrl, UriKind.RelativeOrAbsolute));
But the image is not being displayed.
This won't work. Silverlight runs in a safe Sandbox and you can't just access a file on the desktop.
So you have to call an OpenFileDialog, get the Stream to the file the user selected and set the Stream as source of the BitmapImage.
Add a Button in XAML and do the following in the Click event handler:
private void Button_Click(object sender, RoutedEventArgs e)
{
OpenFileDialog openFileDlg = new OpenFileDialog();
if (openFileDlg.ShowDialog().Value)
{
using (var stream = openFileDlg.File.OpenRead())
{
var bitmapImage = new BitmapImage();
bitmapImage.SetSource(stream);
imgSilverPart.Source = bitmapImage;
}
}
}
As an alternative it's possible to use some special folders if your application runs in elevated trust mode as Out-Of-Browser app.
Maybe the kind of Uri was not determined corectly. Try tu use UriKind.Relative or UriKind.Absolute with valid relative or absolute url string.