Silverlight ImageTools.IO.Jpeg.JpegDecoder Bad Quality - c#

I'm using ImageTools for Silverlight to load a JPG image, but the decoded image quality is BAD (no anti-aliasing, see the second image in the red square).
Here is my code:
OpenFileDialog dlg = new OpenFileDialog();
if (dlg.ShowDialog() == true)
{
var stream = dlg.File.OpenRead();
var newImg = new ExtendedImage(); // ExtendedImage is a ImageTools Api class
var d= new ImageTools.IO.Jpeg.JpegDecoder();
d.Decode(newImg, stream);
image1.Source = newImg.ToBitmap(); //image1 is a System.Windows.Controls.Image
}
Source image
Bad result
Observations
If I set image1.source directly to a URL from the original image, the image is rendered correctly!
Is this a bug in the ImageTools API?
The problem is posted on Codeplex, but it doesn't have any answers.
If I rewrite my code, I get the same result.
XAML
<UserControl x:Class="JPGDecoder.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="400">
<Grid x:Name="LayoutRoot" Background="White">
<Image Height="120" HorizontalAlignment="Left" Margin="46,75,0,0" Name="image1" Stretch="Fill" VerticalAlignment="Top" Width="160" Source="/JPGDecoder;component/Images/org.jpg" />
<Image Height="120" HorizontalAlignment="Left" Margin="212,75,0,0" Name="image2" Stretch="Fill" VerticalAlignment="Top" Width="160" />
<Button Content="Decode JPG from File Stream" Height="23" HorizontalAlignment="Left" Margin="44,25,0,0" Name="button1" VerticalAlignment="Top" Width="192" Click="button1_Click" />
</Grid>
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using ImageTools;
namespace JPGDecoder
{
public partial class MainPage : UserControl
{
public MainPage()
{
InitializeComponent();
}
private void button1_Click(object sender, RoutedEventArgs e)
{
var dlg = new OpenFileDialog();
if (dlg.ShowDialog()==true)
{
var stream = dlg.File.OpenRead();
var newImg = new ExtendedImage();
var d = new ImageTools.IO.Jpeg.JpegDecoder();
d.Decode(newImg, stream);
image2.Source = newImg.ToBitmap();
}
}
}
}
Result

Mmm... I think it would be a good idea if you post a question to the ImageTool codeplex forum, this author used to answer quite fast.
Have you checked the samples from his site?
Usually I had no problems with this library.
Cheers
Braulio

ImageTools does not support antialising, so I got FJCore from Subversion and ran the sample app.
First test same result, bad quality.
Looking at the source code, I found this code block:
// Resize
DecodedJpeg jpegOut = new DecodedJpeg(
new ImageResizer(jpegIn.Image).Resize(320, ResamplingFilters.NearestNeighbor),
jpegIn.MetaHeaders); // Retain EXIF details
and changed it to this:
//Resize
DecodedJpeg jpegOut = new DecodedJpeg(
new ImageResizer(jpegIn.Image).Resize(320, ResamplingFilters.LowpassAntiAlias),
jpegIn.MetaHeaders); // Retain EXIF details
This is the solution: ResamplingFilters.LowpassAntiAlias
Thanks everyone!!

Related

Image display from list of images in a folder C# UWP

I am currently working on building a UWP application that stores music and want it to display an album image that the user can upload. I have figured out how to save the images into a file, but am unable to get them to display, even after trying to bind them to the image control in XAML. I'm fairly new to this, so I apologize if it's a simple fix.
Here's my code from my MainPage.xaml.cs:
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using System.Text;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.Media.Core;
using Windows.Storage;
using Windows.Storage.FileProperties;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Media.Imaging;
using Windows.UI.Xaml.Navigation;
// The Blank Page item template is documented at https://go.microsoft.com/fwlink/?LinkId=402352&clcid=0x409
namespace MyMusicLibrary
{
/// <summary>
/// An empty page that can be used on its own or navigated to within a Frame.
/// </summary>
public sealed partial class MainPage : Page
{
public MainPage()
{
this.InitializeComponent();
}
private async void ImageButton_Click(object sender, RoutedEventArgs e)
{
var picker = new Windows.Storage.Pickers.FileOpenPicker();
picker.ViewMode = Windows.Storage.Pickers.PickerViewMode.Thumbnail;
picker.SuggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.PicturesLibrary;
picker.FileTypeFilter.Add(".jpg");
picker.FileTypeFilter.Add(".jpeg");
picker.FileTypeFilter.Add(".png");
var image = await picker.PickSingleFileAsync();
var folder = ApplicationData.Current.LocalFolder;
var imageFolder = await folder.CreateFolderAsync("imagefolder", CreationCollisionOption.OpenIfExists);
if (imageFolder != null && image != null)
{
var newImage = await image.CopyAsync(imageFolder, image.Name, NameCollisionOption.GenerateUniqueName);
}
}
And here is my code from MainPage.xaml:
<Grid.Background>
<ImageBrush Stretch="Fill" ImageSource="Assets/headphones.jpg"/>
</Grid.Background>
<Button Content="Add Music" HorizontalAlignment="Center" BorderBrush="Black" Background="Gray" Foreground="White" Margin="50,50,50,50" VerticalAlignment="Top" Click="MusicButton_Click"/>
<Button Content="Add Album Cover" HorizontalAlignment="Center" BorderBrush="Black" Background="Gray" Foreground="White" Margin="50,100,50,50" VerticalAlignment="Top" Click="ImageButton_Click"/>
<Image x:Name="newImage" HorizontalAlignment="Center" Width ="300" Height="300" Margin="0,100,0,0" />
<MediaPlayerElement x:Name="mediaPlayer" AreTransportControlsEnabled="True" Margin="0,200,0,0" />
newImage.ImageSource = ...
Full tutorial: https://learn.microsoft.com/en-us/uwp/api/windows.ui.xaml.controls.image.source
You can try this code,
<Image Source="ms-appx:///Assets/logo.png" />
Make sure that Image Properties set "Build Action" to "Content" and "Copy To Output Directory" to "Copy always".
Thanks!!!

Getting image file path from PreviewMouseDown event (WPF/C#)

I'm making an image viewer. I have a mouse event that is supposed to trigger when an image (or child of StackPanel in this case), is clicked. The event is triggered as intended. The problem is, since the pathfile of the images is different depending on whichever is selected (in which directory, etc.), I just don't know how to get the pathfile of the specific image that is currently clicked on. The directory depends on whichever folder is loaded, so the pathfile could be different everytime.
If you scroll all the way down to Image_Load(), I have the Pic.Fill which is a reference to the rectangle that is being filled with the image. I'm trying to find the first parameter for Uri(_,_), which is supposed to be where the #"path" of the current image is.
using System;
using System.IO;
using Microsoft.Win32;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Drawing;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Forms;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace WpfApp1
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
FolderBrowserDialog fbd = new FolderBrowserDialog();
List<string> paths = new List<string>();
string[] extension = new[] { ".jpg", ".png", ".jpeg", ".gif", ".bmp", ".tif", ".tiff" };
public MainWindow()
{
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e) {
if (fbd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
string[] files = Directory.GetFiles(fbd.SelectedPath);
FileLocation.Text = fbd.SelectedPath;
}
}
private void OpenButton_Click(object sender, RoutedEventArgs e)
{
LoadPhotos();
}
private void LoadPhotos()
{
try
{
DirectoryInfo di = new DirectoryInfo(fbd.SelectedPath);
foreach (var fi in di.GetFiles().Where(f => extension.Contains(f.Extension.ToLower())).ToArray())
{
paths.Add(fi.FullName);
}
Photos.ItemsSource = paths;
} catch (Exception e)
{
Console.WriteLine("File not found.", e.Source);
}
}
private void Image_Load(object sender, MouseButtonEventArgs e)
{
// What is the reference here?
Pic.Fill = new ImageBrush { ImageSource = new BitmapImage(new Uri(/*Insert pathfile here*/, UriKind.Absolute)) };
}
}
}
Here's my .xaml. I've analyzed it to see a reference but the only thing that stands out is the Image Source, which I've tagged with a comment. It's set to { Binding . } which I'm going to assume means, whatever image is referenced. But how do I connect that variable reference to my Image_Load()? (which is declared on the instantiation of ListBox)
<Window x:Class="WpfApp1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApp1"
mc:Ignorable="d"
Title="MainWindow" Height="1081" Width="1720.5">
<Grid Margin="0,0,2,-0.578">
<Button x:Name="OpenButton" Content="Load" HorizontalAlignment="Left" Height="44" Margin="1166.374,932.042,0,0" VerticalAlignment="Top" Width="145.666" Background="#FFDDDDDD" FontSize="24" Click="OpenButton_Click"/>
<Button x:Name="BrowseButton" Content="Browse" HorizontalAlignment="Left" Height="44" Margin="58.666,932.042,0,0" VerticalAlignment="Top" Width="134.166" Background="#FFDDDDDD" FontSize="24" Click="Button_Click"/>
<Rectangle x:Name="Pic" Fill="#FFF4F4F5" HorizontalAlignment="Left" Height="878.767" Margin="58.666,34.733,0,0" Stroke="Black" VerticalAlignment="Top" Width="1253.374"/>
<ScrollViewer VerticalScrollBarVisibility="Visible" Width="327.46" HorizontalAlignment="Left" Margin="1339.04,34.733,0,137.078" RenderTransformOrigin="0.5,0.5" >
<StackPanel Width="327.46" RenderTransformOrigin="0.516,0.496" HorizontalAlignment="Left" VerticalAlignment="Center" Height="878.085">
<ListBox x:Name="Photos"
Grid.Row="2" Grid.Column="1" Height="876" PreviewMouseDown="Image_Load">
<ListBox.ItemTemplate>
<DataTemplate>
<Grid Width="300" Height="100">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
/* Image Source? */
<Image Source="{Binding .}" Grid.Column="0"/>
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</StackPanel>
</ScrollViewer>
<TextBlock x:Name="FileLocation" HorizontalAlignment="Left" Height="44" Margin="192.832,948.542,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="973.542" Foreground="#FF515151" TextAlignment="Center" FontSize="14"/>
</Grid>
</Window>
I've been learning on the way using Blend-- some youtube tutorials here and there. I've tried to look for a solution online but all the questions about it usually involve a static and specific directory. This is my first program and the only thing I need is that darn path file reference. There are so many object reference so I just don't even know how to wrap my head around it, being so unfamiliar with these classes. Some assistance would be very much appreciated! :)

WPF - Webbrowser - getElementById

In a WPF application, I have a webbrowser called WebBrowser1. This refers to an HTML page which contains a TextArea to which users can input text.
<html>
<body>
<textarea class="myStudentInput" id="myStudentInput1">
Text to be copied
</textarea>
</body>
</html>
I wish to get this text and potentially also set this text.
I have tried something similar to the javascript way of writing it:
document.getElementById("myStudentOutput1").innerHTML;
such as
HtmlElement textArea = webBrowser1.Document.All["myStudentInput1"];
dynamic textArea = WebBrowser1.Document.GetElementsByID("myStudentInput1").InnerText;
but it doesn't work.
The following solution in Visual Studio 2015 WPF Application works for me.
First, add a reference to the Microsoft HTML COM Library. This is on the COM tab, when you do an "Add Reference" in your project.
Then add the code:
<Window x:Class="WpfApplication3.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApplication3"
mc:Ignorable="d"
Title="MainWindow" Height="600" Width="800">
<Grid>
<WebBrowser x:Name="WebBrowser1" HorizontalAlignment="Left" Height="480" Margin="10,10,0,0" VerticalAlignment="Top" Width="770" Source="E:\Others\Dropbox\Programming\Questions.html"/>
<Button x:Name="mySetQuestionButton" Content="Set Question" HorizontalAlignment="Left" Margin="200,520,0,0" VerticalAlignment="Top" Width="75" Click="mySetQuestion"/>
<Button x:Name="myGetAnswerButton" Content="Get Answer" HorizontalAlignment="Left" Margin="350,520,0,0" VerticalAlignment="Top" Width="75" Click="myGetAnswer"/>
<TextBlock x:Name="textBlock" HorizontalAlignment="Left" Margin="600,520,0,0" TextWrapping="Wrap" Text="Hello2" VerticalAlignment="Top"/>
</Grid>
</Window>
and
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace WpfApplication3
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void mySetQuestion(object sender, EventArgs e)
{
mshtml.HTMLDocument document = (mshtml.HTMLDocument)WebBrowser1.Document;
mshtml.IHTMLElement textArea = document.getElementById("myQuestion1");
textArea.innerHTML = "What is 1+1?";
}
private void myGetAnswer(object sender, EventArgs e)
{
mshtml.HTMLDocument document = (mshtml.HTMLDocument)WebBrowser1.Document;
mshtml.IHTMLElement textArea = document.getElementById("myStudentInput1");
textBlock.Text = textArea.innerHTML;
}
}
}
but it doesn't work.
I have no idea what that could possibly mean. All you can get is a code snippet that does work:
public partial class Form1 : Form {
private WebBrowser webBrowser1;
private Button button1;
public Form1() {
button1 = new Button { Text = "Test" };
button1.Click += button1_Click;
this.Controls.Add(button1);
webBrowser1 = new WebBrowser { Dock = DockStyle.Fill };
webBrowser1.DocumentText = #"<html><body><textarea class=""myStudentInput"" id=""myStudentInput1"">Text to be copied</textarea></body></html>";
this.Controls.Add(webBrowser1);
}
private void button1_Click(object sender, EventArgs e) {
var elem = webBrowser1.Document.GetElementById("myStudentInput1");
MessageBox.Show(elem.InnerText);
}
}
Which produces:

WPF Dynamic Listbox with different output

At first I want to say that i searched on internet and I didn't found anything. I would like to make a dynamic song list, that when user adds a song, new object will be added with name and lenght and when he selects this song he would get a path to the selected song. Here is code to understand better:
XAML:
<Window x:Class="ListBox.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<ListBox x:Name="SongList" HorizontalAlignment="Left" Height="278" Margin="10,10,0,0" VerticalAlignment="Top" Width="248"/>
<TextBlock HorizontalAlignment="Left" Margin="287,124,0,0" TextWrapping="Wrap" Text="Path" VerticalAlignment="Top" Width="194" Height="22"/>
<Label Content="Song Path" HorizontalAlignment="Left" Margin="287,98,0,0" VerticalAlignment="Top" Width="194"/>
<Button Content="Add Song" HorizontalAlignment="Left" Margin="10,293,0,0" VerticalAlignment="Top" Width="132" Click="Button_Click"/>
</Grid>
and code:
using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace ListBox
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
OpenFileDialog openfile = new OpenFileDialog();
openfile.DefaultExt = ".mp3";
openfile.Filter = "mp3 | *.mp3";
Nullable<bool> result = openfile.ShowDialog();
if (result == true)
{
String file = openfile.FileName;
FileInfo fileinfo = new FileInfo(file);
SongList.Items.Add(fileinfo.Name);
}
}
}
}
so where the "Path" text box is I would like to get current selected song path. Is it possible to make with ItemBox or I need to make array that will save all paths?
I think you should use for your songs, because you're writing a WPF application, an ObservableCollection of your song objects, in order to have a proper ListBox with your updated objects.
Then, you have to create a proper Song class with its own properties, like the SongName or the SongPath. In this class, you will implement the INotifyPropertyChanged interface
and, for each property, you will raise an appropriate OnPropertyChanged event.
With this implementation, once you load a song and add it to the songs list, your ListBox will show the updated collection accordingly.
Further info about the ObservableCollection here.
If you want to have a look at a code sample, in this answer I written an example of developing an ObservableCollection.
at first you might want to have a look at MVVM programming. The concept is not that easy to understand, even harder to apply but once you got it you will be able to create good UI in no-time and it's especially perfect for what you are trying to do.
Now to your problem. FileInfo.Name only stores the file name not the path, so if you don't store the path separately, you won't be able to get it back from just the file name.
KillaKem's solution seems pretty good, however I wouldn't recommend using the Tag property for this. Just create a Collections::Generic::List (please, don't use arrays) class member to store the paths and then use the selectedIndexChange Event to update the PathSearchBox.
using MVVM you could just link the ListBox to a Dictionary which stores both, path and filename and just display the Filename, feel free to research MVVM a bit yourself.
Regards,
Xaser
You could try something along the lines of this,
XAML:
<Window x:Class="ListBox.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<ListBox x:Name="SongList" HorizontalAlignment="Left" Height="278" Margin="10,10,0,0" VerticalAlignment="Top" Width="248"/>
<TextBlock HorizontalAlignment="Left" Margin="287,124,0,0" TextWrapping="Wrap" Text="Path" VerticalAlignment="Top" Width="194" Height="22"/>
<Label Name ="pathLabel" Content="Song Path" HorizontalAlignment="Left" Margin="287,98,0,0" VerticalAlignment="Top" Width="194"/>
<Button Content="Add Song" HorizontalAlignment="Left" Margin="10,293,0,0" VerticalAlignment="Top" Width="132" Click="Button_Click"/>
</Grid>
Back Code:
namespace ListBox
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
OpenFileDialog openfile = new OpenFileDialog();
openfile.DefaultExt = ".mp3";
openfile.Filter = "mp3 | *.mp3";
Nullable<bool> result = openfile.ShowDialog();
if (result == true)
{
String file = openfile.FileName;
FileInfo fileinfo = new FileInfo(file);
SongList.Items.Add(fileinfo.Name);
var pathList = SongList.Tag as List<string>;
pathList.Add(fileinfo.DirectoryName);
SongList.Tag = pathList;
}
}
private void Selection_Changed(object sender, EventArgs e)
{
var myListBox = sender as ListBox;
var myPathList = myListBox.Tag as List<string>;
var filePath = myPathList[myListBox.SelectedIndex];
pathLabel.Content = filePath;
}
}
}
Haven't tested the code but it should be working or close to working.
These is a SelectionChanged event in the listbox you can use. But i would recommend using bi

Combobox not visible to code behind in Silverlight

I have been trying to add items to a comboxbox but cannot seem to get the code behind file to recognize the combobox added to the xaml. I am pretty sure I am missing something simple. Basically the xaml here illustrates an empty combobox. The code behind executes the service, waits for json to come back and deserialize it. Unfortunately I cannot get
xaml:
<navigation:Page x:Class="Growing.Views.Room"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
xmlns:navigation="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Navigation"
d:DesignWidth="950" d:DesignHeight="480"
Title="Home" Style="{StaticResource PageStyle}" DataContext="{Binding}" xmlns:toolkit="http://schemas.microsoft.com/winfx/2006/xaml/presentation/toolkit" xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk">
<Grid x:Name="LayoutRoot" ShowGridLines="True" Background="#FF631C00">
<Grid.ColumnDefinitions>
</Grid.ColumnDefinitions>
<Rectangle Height="298" HorizontalAlignment="Left" Margin="195,94,0,0" Name="rect" Stroke="Black" StrokeThickness="2" VerticalAlignment="Top" Width="582" Fill="#FFAAAAAA" RadiusY="0.25" RadiusX="0.25" />
<sdk:Label Height="38" HorizontalAlignment="Left" Margin="387,160,0,0" Name="label1" VerticalAlignment="Top" Width="203" Content="Select a Room" FontSize="24" FontWeight="Bold" />
<sdk:Label Height="18" HorizontalAlignment="Left" Margin="312,240,0,0" Name="label2" VerticalAlignment="Top" Width="69" Content="Area:" FontSize="14" />
<ComboBox x:Name="RoomAreas" Height="23" HorizontalAlignment="Left" Margin="418,235,0,0" VerticalAlignment="Top" Width="209" />
</Grid>
The code behind:
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Runtime.Serialization.Json;
using System.ServiceModel.Web;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Navigation;
using Growing.DataConnectionRef;
namespace Growing.Views
{
public partial class Room : Page
{
public Room()
{
InitializeComponent();
//Asynchronously call the EndReceive Web Service to change the status of an existing open lot record
WebClient GRService = new WebClient();
GRService.DownloadStringCompleted += new DownloadStringCompletedEventHandler(GRService_DownloadStringCompleted);
GRService.DownloadStringAsync(new Uri("/servicestack/GetAreas", UriKind.Relative));
}
static void GRService_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
List<Area> dataList = new List<Area>();
MemoryStream memoryStream = new MemoryStream(Encoding.UTF8.GetBytes(e.Result));
DataContractJsonSerializer ser = new DataContractJsonSerializer(dataList.GetType());
dataList = ser.ReadObject(memoryStream) as List<Area>;
memoryStream.Close();
RoomAreas.ItemSource = dataList;
}
}
}
At RoomAreas.ItemSource I get an error An object reference is required for the non-static field, method, or property 'Growing.Views.Room.RoomAreas'
Sorry if this is hard to follow. Anyone have any ideas what might be going on here?
Thank you in advance!
Make GRService_DownloadStringCompleted method non static:
void GRService_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
List<Area> dataList = new List<Area>();
MemoryStream memoryStream = new MemoryStream(Encoding.UTF8.GetBytes(e.Result));
DataContractJsonSerializer ser = new DataContractJsonSerializer(dataList.GetType());
dataList = ser.ReadObject(memoryStream) as List<Area>;
memoryStream.Close();
RoomAreas.ItemSource = dataList;
}
Try adding ItemsSource="{Binding}" to the combobox property within the Xaml

Categories

Resources