Setting the last background image as the default - c#

I have added four images and by default i have background image. I used a button to change the background randomly. It is panorama page and i just want my app to save the last state(i.e to remember the last background image) and if my app is activated then the last image should be the default background image. Since i have already added some images to my app so i think this doesn't need Isolated Storage. What i need is if the current background image(imguri) is bg1.jpg, and if i exit the app and if i relaunch it then the default background image should be bg1.jpg. Need help!
private void BackgroundBrowser_Click(object sender, RoutedEventArgs e)
{
string imguri = "";
click_count = click_count % 5;
switch (click_count)
{
case 0: imguri = "Image/bg.jpg"; break;
case 1: imguri = "Image/bg1.jpg"; break;
case 2: imguri = "Image/bg3.jpg"; break;
case 3: imguri = "Image/bg2.jpg"; break;
case 4: imguri = ""; break;
}
click_count++;
var app = Application.Current as App;
app.appBmp = new BitmapImage(new Uri(imguri, UriKind.Relative));
ImageBrush imageBrush = new ImageBrush();
imageBrush.Stretch = Stretch.UniformToFill;
imageBrush.Opacity = 0.7;
imageBrush.ImageSource = app.appBmp;
this.LayoutRoot.Background = imageBrush;
app.appbrush = imageBrush;
app.backchanged = true;
}

You can use Application or User Settings. Go to project properties and click the Settings tab. Then create a setting name LastImagePath with String as a type:
Now just before this line:
var app = Application.Current as App;
Add this to save the path to the LastImagePath settings:
Properties.Settings.Default.LastImagePath = imguri;
Properties.Settings.Default.Save();
To load the last image, you can load up the setting wherever you want like this:
if (!(Properties.Settings.Default.LastImagePath == null))
imgpath = Properties.Settings.Default.LastImagePath;

you need save the last image name in a file when your application exit and read image name from the file and load it when your application start again. I think this is the simplest solution.

You can also use System.IO.IsolatedStorage.IsolatedStorageSettings.ApplicationSettings in a similar manner to what DelegateX has shown. Mind that regardless of how do you 'save' your setting, it will be stored in the isolated storage space. It is just nicely wrapped and hidden as a Properties/ApplicationSettings/Session etc proeprties or class names, but in fact the data will land on ISO, and will evaporate when you uninstall the app from the device.

All items stored in the User/Application settings need to be serializable (there is a note at the bottom of the documentation here). Learn more about serialization here.

Related

Custom Accepted Operation in Drag and Drop

I am making a library management software. This is the screenshot:
I have implementing functionalities like dragging a book into the delete icon to delete the book. But there are two obstacles:
DataPackageOperation has only four possibilities: Copy, link, move none. So, after four, it is difficult to differentiate which AppBarButton is the book dropped on.
I plan to add more items to the CommandBar. But there are only four possible operations
I need a way to give the user a custom feedback as to which AppBarButton is the book currently dragged over. DataPackageOperation contains only four. Out of which, 'None' cannot be used(because it will be confusing). Is there a way to provide that feedback?
I need a way to give the user a custom feedback as to which AppBarButton is the book currently dragged over
You could give the user a custom feedback via custom Drag UI. The follow code comes from XamlDragAndDrop official code sample.
private void TargetTextBox_DragEnter(object sender, Windows.UI.Xaml.DragEventArgs e)
{
/// Change the background of the target
VisualStateManager.GoToState(this, "Inside", true);
bool hasText = e.DataView.Contains(StandardDataFormats.Text);
e.AcceptedOperation = hasText ? DataPackageOperation.Copy : DataPackageOperation.None;
if (hasText)
{
e.DragUIOverride.Caption = "Drop here to insert text";
// Now customize the content
if ((bool)HideRB.IsChecked)
{
e.DragUIOverride.IsGlyphVisible = false;
e.DragUIOverride.IsContentVisible = false;
}
else if ((bool)CustomRB.IsChecked)
{
var bitmap = new BitmapImage(new Uri("ms-appx:///Assets/dropcursor.png", UriKind.RelativeOrAbsolute));
// Anchor will define how to position the image relative to the pointer
Point anchor = new Point(0,52); // lower left corner of the image
e.DragUIOverride.SetContentFromBitmapImage(bitmap, anchor);
e.DragUIOverride.IsGlyphVisible = false;
e.DragUIOverride.IsCaptionVisible = false;
}
// else keep the DragUI Content set by the source
}
}

Change background image source on button click in wpf forms

My default background image is "lobby.jpg" and when I click the "Lights" button I want it to swap with "lobby1.jpg" and vice versa. These images are stored in "obj\Debug\Images\".
Also I'd like to implement relative(?) imagesource uris so that I can access the images on any machine (without using the whole uri, just the "obj\Debug\Images\").
Edit: So the main issue seems to be that I tried changing the window background without realising that it was getting "covered" by the grid background of the page. So what I did is I set the main window background to "lobby.jpg", I made the grid background invisible and used the code from the answer to swap between the 2 backgrounds.
You can use AppDomain basepath to exe (this is are simplest way)
var basePath= AppDomain.CurrentDomain.BaseDirectory;
var imageDirPath = $"{basePath}\\Images\\";
Example:
bool clicked = false;
private void button_Click(object sender, RoutedEventArgs e)
{
var basePath = AppDomain.CurrentDomain.BaseDirectory;
var imageDirPath = $"{basePath}\\Images\\";
if (clicked)
image.Source = new BitmapImage(new Uri(imageDirPath+ "lobby.jpg"));
else
image.Source = new BitmapImage(new Uri(imageDirPath + "lobby1.jpg"));
clicked = !clicked;
}

Change default tile image url

Is there any way I could change the Uris of the default tile images in the WMAppManifest.xml at runtime?
For example I would like to enable an option for users to select the tile image in the settings of my app. This is not the problem because I can then update the main tile with the new image if the app is pinned to the start, but if the app is not pinned and the user wants to pin the app another time then the default image will be used, and this is not the behavior I want, I want the tile to have the image the user selected in the settings. How to achieve this if possible?
I figured it out, I didn't know the ShellTile.ActiveTiles would always contain the default tile, whether the app is pinned or not, so I just updated this tile when the settings item changed:
private async void UpdateTile(bool isTransparent)
{
ShellTile defaultTile = ShellTile.ActiveTiles.FirstOrDefault();
if (defaultTile != null)
{
string tileFolder = isTransparent ? "Transparent" : "Normal";
defaultTile.Update(new FlipTileData()
{
SmallBackgroundImage = new Uri("appdata:/Assets/Tiles/" + tileFolder + "/Logo.scale-100.png"),
BackgroundImage = new Uri("appdata:/Assets/Tiles/" + tileFolder + "/Logo.scale-180.png"),
WideBackgroundImage = new Uri("appdata:/Assets/Tiles/" + tileFolder + "/WideLogo.scale-180.png")
});
}
}

Couldn't assign value in FileUpload tool

I grabbed the value from database and I am trying to assign those values in Edit Form. But the only problem is with the FileUpload. It don't take the value. Can anyone suggest me what I'm missing here
private void EditForDataByID(int TitleId)
{
ReadmoreController objFormController = new ReadmoreController();
ReadMoreInfo objInfo = objFormController.GetListObjectOfAllArticle(TitleId);
if (objInfo != null)
{
TextTitle.Text = objInfo.Title;
txtSummary.Text = objInfo.Summary;
TextDate.Text = objInfo.Date.ToString();
//FileUpload1.FileName=objInfo.Image; I even tried this but it doesn't work
FileUpload1 = objInfo.Image;
Session["TitleId"] = TitleId;
ListDiv.Visible = false;
form.Visible = true;
BindGrid();
}
}
For client security reason you can not assign value to FileUploadControl as it could cause the uploading of unwanted files from client machine. So let the use pick the file to upload.
If it is allowed then one can steel the important files from client machine like c:\PersonalPasswords
Edit Based on comments
If you need to ensure that user has selected an Image and does not need to change it then you can use a image control and assign image to it. Use the same image control to find if the image is selected or not.

Dynamically-Added Custom User Controls Not Visible

I have a form in C# with a drop down box. When that drop down box gets changed, I want it to load a custom user control into a panel. I get no compile errors, no runtime errors, but I also get no visible user control. The idea here is that each user control understands the configuration for a different scenario. I want to encapsulate each scenario in its own control and the drop down box allows the user to select the scenario, which loads the control for the user to perform configuration. By adjusting the options in the drop down, I can customize what scenarios a given customer has. As I said, my problem is I cannot get any of the controls to become visible. This is the code I am using for the drop down index change event handler:
private void ddlCollectorType_SelectedIndexChanged (object sender, EventArgs e)
{
m_currentControl = null;
pnlDeviceConfig.Controls.Clear ();
switch ((string) ddlCollectorType.SelectedItem)
{
case "SEL-421":
SEL421ASCIIControl s421 = new SEL421ASCIIControl (this);
m_currentControl = s421;
pnlDeviceConfig.Controls.Add (s421);
break;
case "SEL-421 (FTP)":
break;
case "GE D60":
GED6061850Control geD60 = new GED6061850Control (this);
m_currentControl = geD60;
pnlDeviceConfig.Controls.Add (geD60);
break;
case "GE D60 (TFTP)":
break;
case "MiCOM P442":
break;
}
}
I've only created a couple of the user controls so far, hence the empty case statements. When I make selections that should show me something, I get nothing (confirmed in the debugger that I am hitting the case statement body). Any help will be greatly appreciated!
To follow up on my comment, I'm guessing that the position and/or dimensions of your control are not set.
Try something like:
...
SEL421ASCIIControl s421 = new SEL421ASCIIControl (this);
m_currentControl = s421;
pnlDeviceConfig.Controls.Add (s421);
// TODO: Set real size and position.
s421.Left = 0;
s421.Top = 0;
s421.Width = 100;
s421.Height = 50;
break;
...
You also may use the Dock and Anchor properties of your control.
1 You can replace your Menu with PlaceHolder, he is ideal control for adding controls
here an example : http://www.developerfusion.com/code/3826/adding-controls-to-placeholders-dynamically/
2 And for your case you can try to adjust visible property of panel to true

Categories

Resources