|
Is this for a WPF app?
"WPF has many lovers. It's a veritable porn star!" - Josh Smith As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.
My blog | My articles | MoXAML PowerToys | Onyx
|
|
|
|
|
Welllll, setup three BackgroundWorkers - one for each progressbar.
"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997 ----- "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001
|
|
|
|
|
Thanks for your quick replies,
I am posting my code. This code is in code behind of WPF window. UI has four ProgressBars which I have to update.
public static List<photopost> allPhotosToUpload = null;
private BackgroundWorker serviceValuesWorker = null;
private BackgroundWorker signatureWorker = null;
private BackgroundWorker photoWorker = null;
private BackgroundWorker mainWorker = null;
public UploadProgressWithfourBars()
{
InitializeComponent();
UploadServiceValuesPhotosAndSignature();
}
private void UploadServiceValuesPhotosAndSignature()
{
bool finalSuccessMessage = false;
var context = new VersentiaMobileDataContext();
var serviceInstances = ServiceInstancesDataAccess.GetAll(context);
bool[] allResponses;
List<signature> signatures = null;
foreach (var serviceInstance in serviceInstances)
{
//Get All Signatures
var serviceValues = serviceInstance.ServiceValues.Where(sv => sv.ServiceProperty.Datatype == "Signature").Select(sv =>sv);
signatures = serviceValues.Select(sv => sv.Signature).Distinct().ToList();
//Get All Photos
allPhotosToUpload = PostPhotoCollection.GetPhotos(serviceInstance, context);
int count = allPhotosToUpload.Count + signatures.Count + 1;
allResponses = new bool[count];
int i = 0;
//Upload serviceValues
serviceValuesWorker = new BackgroundWorker();
serviceValuesWorker.WorkerReportsProgress = true;
serviceValuesWorker.WorkerSupportsCancellation = true;
serviceValuesWorker.DoWork += delegate(object s, DoWorkEventArgs args)
{
serviceValuesWorker.ReportProgress(10);
if (serviceValuesWorker.CancellationPending)
{
args.Cancel = true;
return;
}
serviceValuesWorker.ReportProgress(50);
var serializableValues = serviceInstance.ServiceValues.AsSerializable();
var serialisedValues = new Dictionary<string, object[]=""> { { "ValuesInfo", serializableValues } }.Serialize();
string responseFromServer = ServiceRequest.GetResponse("UpdateServiceInstanceMobile", "ServiceInstances", serialisedValues, serviceInstance.ServiceInstanceID.ToString());
var response = responseFromServer.Deserialize<<dictionary>string, object>>();
bool successServiceValues = (bool)response["success"];
if (successServiceValues)
{
serviceValuesWorker.ReportProgress(100);
}
args.Result = successServiceValues;
};
serviceValuesWorker.ProgressChanged += delegate(object s, ProgressChangedEventArgs args)
{
serviceValuesProgressBar.Value = args.ProgressPercentage;
};
serviceValuesWorker.RunWorkerCompleted += delegate(object s, RunWorkerCompletedEventArgs args)
{
try
{
allResponses[i++] = (bool) args.Result;
}
catch (Exception)
{
}
};
serviceValuesWorker.RunWorkerAsync();
//Upload Signatures
signatureWorker = new BackgroundWorker();
signatureWorker.WorkerReportsProgress = true;
signatureWorker.WorkerSupportsCancellation = true;
signatureWorker.DoWork += delegate(object s, DoWorkEventArgs args)
{
signatureWorker.ReportProgress(10);
if (signatureWorker.CancellationPending)
{
args.Cancel = true;
return;
}
signatureWorker.ReportProgress(50);
bool[] responseArray = new bool[signatures.Count];
int j = 0;
foreach (var signature in signatures)
{
string serviceValuesString = "";
var serviceValuesIDs = serviceValues.Where(sv => sv.SignatureID == signature.SignatureID).Select(sv => sv.ServiceValueID).ToList();
foreach (var ID in serviceValuesIDs)
{
if (ID != serviceValuesIDs.Last())
{
serviceValuesString = serviceValuesString + ID + ",";
}
else
{
serviceValuesString = serviceValuesString + ID;
}
string signaturePath = signature.SignaturePath;
byte[] signatureByteArray = SignatureFileConversion.SignatureFileToByteArray(signaturePath);
string signatureToUpload = signatureByteArray.Serialize();
var signatureResponseFromServer = ServiceRequest.GetSignatureResponse("AddSignature", "ServiceInstances", serviceValuesString, serviceInstance.ServiceInstanceID.ToString(), signatureToUpload);
var signatureResponse = signatureResponseFromServer.Deserialize<<dictionary>string, object>>();
responseArray[j++] = (bool)signatureResponse["success"];
}
}
if(!responseArray.Contains(false))
{
signatureWorker.ReportProgress(100);
}
args.Result = responseArray;
};
signatureWorker.ProgressChanged += delegate(object s, ProgressChangedEventArgs args)
{
signatureProgressBar.Value = args.ProgressPercentage;
};
signatureWorker.RunWorkerCompleted += delegate(object s, RunWorkerCompletedEventArgs args)
{
try
{
bool[] signatureResponses = (bool[]) args.Result;
foreach (var response in signatureResponses)
{
allResponses[i++] = response;
}
}
catch (Exception)
{
}
};
signatureWorker.RunWorkerAsync();
//Upload Photos
photoWorker = new BackgroundWorker();
photoWorker.WorkerReportsProgress = true;
photoWorker.WorkerSupportsCancellation = true;
photoWorker.DoWork += delegate(object s, DoWorkEventArgs args)
{
photoWorker.ReportProgress(10);
if (photoWorker.CancellationPending)
{
args.Cancel = true;
return;
}
photoWorker.ReportProgress(50);
bool[] photoResponseArray = new bool[allPhotosToUpload.Count];
int k = 0;
foreach (var photo in allPhotosToUpload)
{
photoResponseArray[k++] = PostPhotoCollection.SendPhoto(photo);
}
if(!photoResponseArray.Contains(false))
{
photoWorker.ReportProgress(100);
}
args.Result = photoResponseArray;
};
photoWorker.ProgressChanged += delegate(object s, ProgressChangedEventArgs args)
{
serviceValuesProgressBar.Value = args.ProgressPercentage;
};
photoWorker.RunWorkerCompleted += delegate(object s, RunWorkerCompletedEventArgs args)
{
try
{
bool[] photoResponses = (bool[]) args.Result;
foreach (var response in photoResponses)
{
allResponses[i++] = response;
}
}
catch (Exception)
{
}
};
photoWorker.RunWorkerAsync();
mainWorker = new BackgroundWorker();
mainWorker.WorkerReportsProgress = true;
mainWorker.WorkerSupportsCancellation = true;
mainWorker.DoWork += delegate(object s, DoWorkEventArgs args)
{
mainWorker.ReportProgress(10);
if (mainWorker.CancellationPending)
{
args.Cancel = true;
return;
}
mainWorker.ReportProgress(50);
if (!allResponses.Contains(false))
{
bool IsDone = true;
string finalResponseFromServer = ServiceRequest.GetFinalResponse("SubmitChanges", "ServiceInstances", IsDone.ToString(), serviceInstance.ServiceInstanceID.ToString(), allPhotosToUpload.Count.ToString(), signatures.Count().ToString());
var finalResponse = finalResponseFromServer.Deserialize<<dictionary>string, object>();
finalSuccessMessage = (bool)finalResponse["success"];
args.Result = finalSuccessMessage;
if (finalSuccessMessage)
{
mainWorker.ReportProgress(100);
}
}
};
mainWorker.ProgressChanged += delegate(object s, ProgressChangedEventArgs args)
{
mainProgressBar.Value = args.ProgressPercentage;
};
mainWorker.RunWorkerCompleted += delegate(object s, RunWorkerCompletedEventArgs args)
{
try
{
if((bool) args.Result)
{
btnClose.IsEnabled = true;
}
}
catch (Exception)
{
}
};
mainWorker.RunWorkerAsync();
btnCancel.Click += ((o, args) =>
{
serviceValuesWorker.CancelAsync();
serviceValuesProgressBar.Value = 0;
signatureWorker.CancelAsync();
signatureProgressBar.Value = 0;
photoWorker.CancelAsync();
photoProgressBar.Value = 0;
mainWorker.CancelAsync();
mainProgressBar.Value = 0;
bool IsDone = false;
string finalResponseFromServer = ServiceRequest.GetFinalResponse("SubmitChanges", "ServiceInstances", IsDone.ToString(), serviceInstance.ServiceInstanceID.ToString(), allPhotosToUpload.Count.ToString(), signatures.Count().ToString());
var finalResponse = finalResponseFromServer.Deserialize<dictionary<string, object="">>();
finalSuccessMessage = (bool)finalResponse["success"];
if (!finalSuccessMessage)
{
lblUploadCancelled.Visibility = Visibility.Visible;
btnClose.IsEnabled = true;
btnCancel.IsEnabled = false;
}
});
}
}
private void Click_Close(object sender, RoutedEventArgs e)
{
this.Close();
}
I am getting Accessviolation Exception. "allResponses" array is not modifying at all. The same procedure if I do without threads working fine, but no progress indication to the user.
Please help.
|
|
|
|
|
Nobody's going to help you until a) your code is properly formatted, and b) you can whittle it down to something that doesn't require more than one scrolled page.
Lastly, this is the WPF/WCF/WWF forum. How is your question related to any of these topics?
"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997 ----- "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001
|
|
|
|
|
vsaratkar wrote: I am getting Accessviolation Exception. "allResponses" array
I'm not going to put much effort into reading that big chunk of unformatted code,
but to start with, it looks like you have multiple threads accessing the "allResponses"
array and the "i" variable with no synchronization.
As for the exception - this should be easy to debug. Either the array is null or you're
trying to write past the end (i.e. the array is not large enough)...
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
I am getting a eroor from my code, i cant trace the issue/bug . here is my code bit...
private void DoArrange()<br />
{<br />
Point center = new Point((this.Width - ItemSize) / 2, (this.Height - ItemSize) / 2);<br />
double radiusX = center.X;<br />
double radiusY = center.Y;<br />
double scale = ScalePerspective;<br />
<br />
for (int i = 0; i < Children.Count; i++)<br />
{<br />
UIElement item = Children[i];<br />
double radians = (double)item.GetValue(CarouselPanel.AngleProperty);<br />
<br />
Point p = new Point(<br />
(Math.Cos(radians) * radiusX) + center.X,<br />
(Math.Sin(radians) * radiusY) + center.Y<br />
);<br />
<br />
if (item.RenderTransform == null)<br />
{<br />
item.RenderTransform = new MatrixTransform();<br />
item.RenderTransformOrigin = new Point(0.5, 0.5);<br />
}<br />
MatrixTransform mt = item.RenderTransform as MatrixTransform;<br />
double scaleMinusRounding = p.Y / (center.Y + radiusY);<br />
double scaleX = Math.Min(scaleMinusRounding + scale, 1.0);<br />
double scaleY = Math.Min(scaleMinusRounding + scale, 1.0);<br />
Matrix mx = new Matrix(scaleX, 0.0, 0.0, scaleY, 0.0, 0.0);<br />
<br />
mt.Matrix = mx;<br />
item.RenderTransform = mt;<br />
<br />
int zIndex = (int)((p.Y / base.Height) * 50);<br />
item.SetValue(Canvas.ZIndexProperty, zIndex);<br />
Rect r = new Rect(p.X, p.Y, ItemSize, ItemSize);<br />
item.Arrange(r);<br />
}<br />
}
What could be the issue i am using a WPF(win) application.
modified on Thursday, July 9, 2009 9:48 AM
|
|
|
|
|
I can't see the Identity property in your code. Are you sure this is where the problem is?
"WPF has many lovers. It's a veritable porn star!" - Josh Smith As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.
My blog | My articles | MoXAML PowerToys | Onyx
|
|
|
|
|
I edidted the post again,..the error raised from the underlined statement..
|
|
|
|
|
Basically, you have a frozen object here (you can tell this by checking mt.Matrix.IsFrozen). Once an object is frozen, you can't modify it - instead, you have to clone it and work with that.
"WPF has many lovers. It's a veritable porn star!" - Josh Smith As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.
My blog | My articles | MoXAML PowerToys | Onyx
|
|
|
|
|
Pete O'Hanlon wrote: (you can tell this by checking mt.Matrix.IsFrozen).
i cannot find any handle like that, but i found something mt.Matrix.IsIdentity which is returns a bool value. What it does?..
Your corperation is really appreciated!
|
|
|
|
|
ASysSolvers wrote: i cannot find any handle like that, but i found something mt.Matrix.IsIdentity which is returns a bool value. What it does?..
This property identifies whether or not a matrix is the identity matrix. Basically, an identity matrix is a square matrix which has 1s on the main diagonal and 0s elsewhere.
"WPF has many lovers. It's a veritable porn star!" - Josh Smith As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.
My blog | My articles | MoXAML PowerToys | Onyx
|
|
|
|
|
Do you have any idea to modify my code in the correct way?.. im struggled now.. i dont knw,.. i want to use this in WPF(Windows) application,..but fine in web it works,..
In windows it gives runtine error like in Thread subject..
|
|
|
|
|
Hello,
Currently im hanging on one Problem. I wrote a class inherting from grid. Each grid has the property
children. How can i get an notification when a new children added to the grid. I need somethink like a
event which occours when the children-cout changed. I hope you understand what i mean.
Thanks in advanced
|
|
|
|
|
You're looking at the ObservableCollection - that notifies you when an item has been added or removed from a collection.
"WPF has many lovers. It's a veritable porn star!" - Josh Smith As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.
My blog | My articles | MoXAML PowerToys | Onyx
|
|
|
|
|
The Children property of a Grid is a UIElementCollection, which is not Observable, and is sealed.
So I have the same question - is there any way to see if the Children collection in a Panel has changed?
|
|
|
|
|
I am using wpf toolkit:Datagrid in my application with one Check box column and another textbox column. By default textbox column is Readonly.If Check box is checked then editing the textbox for particular row.
How can achieve the above scenario?...Thanks in advance
|
|
|
|
|
Does anyone know where to find the complete default control template xaml listings for the WPF controls?
Thanks.
Bob
|
|
|
|
|
The WPF documentation doesn’t list the XAML for standard control templates. But, you can write program to get the control template and then modify it and Re-Apply it.
Here is the program to get the control Template Markup of a given control.
In a Main window take a listbox control to enlist all the controls, and when any item is selected the corrosponding Control Template's Markup is displayed in a Textblock control.
Here is the WPF program to demonstrate the same
This is the XAML part
<Window x:Class="DefaultControlTemplate.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="462" Width="572" Loaded="Window_Loaded" >
<Grid>
<ListBox Margin="12,12,0,12" Name="listBox1" HorizontalAlignment="Left" Width="168" SelectionChanged="listBox1_SelectionChanged" />
<TextBlock HorizontalAlignment="Right" Margin="0,14,12,10" Name="textBlock1" Width="159" />
</Grid>
</Window>
This is how .cs file would look like
private void Window_Loaded(object sender, RoutedEventArgs e)
{
Type controlType = typeof(Control);
List<Type> ctrlTypes = new List<Type>();
//searches all the types in a dll where generic control class is defined
Assembly assembly = Assembly.GetAssembly(typeof(Control));
foreach (Type type in assembly.GetTypes())
{
// Only add a type of the list if it's a Control, a concrete class,
// and public.
if (type.IsSubclassOf(controlType) && !type.IsAbstract && type.IsPublic)
{
ctrlTypes.Add(type);
}
}
listBox1.ItemsSource = ctrlTypes;
}
------------------------------------------------------------
private void listBox1_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
try
{
// Get the selected type.
Type type = (Type)listBox1.SelectedItem;
// Instantiate the type.
ConstructorInfo info = type.GetConstructor(System.Type.EmptyTypes);
Control control = (Control)info.Invoke(null);
// Get the template.
ControlTemplate template = control.Template;
// Get the XAML for the template.
XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true;
StringBuilder sb = new StringBuilder();
XmlWriter writer = XmlWriter.Create(sb, settings);
XamlWriter.Save(template, writer);
textBlock1.Text = sb.ToString();
}
catch (Exception err)
{
textBlock1.Text = "<< Error generating template: " + err.Message + ">>";
}
}
---------------------------------------------------------
Let me explain the code:
At the Window_loaded event, I have enlisted all the controls in a list box.
When any of the items from list box is selected, I have created the control as per selection, got it's control template and serialized into XML and displayed the same in textblock as text.
If you want, you can start with a simpler program, say a window has a button and a textblock. When clicked on button,
the corrosponding XAML for button's control template will be displayed in textblock. In this case, Button is instantiated, hence need to created Button Object, as shown below.
private void button1_Click(object sender, RoutedEventArgs e)
{
ControlTemplate template = ((Control)sender).Template;
XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true;
StringBuilder sb = new StringBuilder();
XmlWriter writer = XmlWriter.Create(sb, settings);
XamlWriter.Save(template, writer);
label1.Content = sb.ToString();
}
|
|
|
|
|
I ran your first version which looks promising, only the line:
ControlTemplate template = control.Template;
Always assigns a null value to the template variable.
The error message is typically:
<< Error generating template: Parameter name: obj>>
Are you seeing the same thing?
Thanks for your reply,
Bob
|
|
|
|
|
The error is obviously thrown by the line:
XamlWriter.Save(template, writer);
since template is null.
|
|
|
|
|
It appears that when you construct a WPF control in code, its Template property isn't initialized. For example:
ListBox listBox = new ListBox();
If you instantiate a ListBox like this, then walk the object hierarchy down to the Template property of the Control base class, you'll see that it hasn't been initilaized.
|
|
|
|
|
Turns out, after a little more exploration, that the Template property will be null unless the control has been rendered on the screen.
|
|
|
|
|
I know it sounds stupid but i'm looking for a way to style a textbox so that when it has focus it selects all the text. I know I could just call 'textBox.SelectAll()' (but i like to avoid x:Name). I could subclass the textbox and override OnFocus and call "SelectAll()" (but i don't like the idea). I'd really like to be able to do this in xaml by creating a style. I did a google check but came up empty
EDIT:
i just came across this solution[^]. I'm really hoping for a pure xaml solution but this will work.
Don't be overcome by evil, but overcome evil with good
|
|
|
|
|
|
|