Cant Make instances with usercontrol
In Windows Phone7 I created an UserControl in which I am parsing some Data. Now I am not able to make instances of that Usercontrol.If I drag n drop the usercontrol the error occurs.
When I remove parsing from the Usercontrol I am able to make instances unless I am getting error such as
[CODE]`"Cannot create instance of '<usercontrolname>'
"XapCannotFindFileInPackage]
Arguments:HelpScreenData.xml
Debugging Resource String are unavailable.Often the key and
arguments provide sufficient information to diagonise the problem.see
http://go.microsoft.com'/fwlink/?linkid=106663&version5.0.10411.00&
file=system.xml.dll&key=Xml_cannot find file in Xappackage"[/CODE]
And my UserControl is
[CODE]
public partial class ques : UserControl
{
Popup popup = new Popup();
tutorial tut = new tutorial(); //another usercontrol
String[] str;
String[] nam;
public ques()
{
InitializeComponent();
}
private void UserControl_Loaded(object sender, RoutedEventArgs e)
{
LayoutRoot.Visibility = Visibility.Visible;
buy_bac.Visibility = Visibility.Collapsed;
buy_hme.Visibility = Visibility.Collapsed;
buy_qestim.Visibility = Visibility.Collapsed;
buy_piti.Visibility = Visibility.Collapsed;
buy_qual.Visibility = Visibility.Collapsed;
buy_rent.Visibility = Visibility.Collapsed;
buy_rep.Visibility = Visibility.Collapsed;
buy_mre.Visibility = Visibility.Collapsed;
if (Modules.Programname == "Buyers")
{
buy_bac.Visibility = Visibility.Visible;
buy_hme.Visibility = Visibility.Visible;
buy_qestim.Visibility = Visibility.Visible;
buy_piti.Visibility = Visibility.Visible;
buy_qual.Visibility = Visibility.Visible;
buy_rent.Visibility = Visibility.Visible;
buy_rep.Visibility = Visibility.Visible;
buy_mre.Visibility = Visibility.Visible;
}
XDocument doc = XDocument.Load("HelpScreenData.xml");
string val = doc.ToString();
XElement doc1 = doc.Descendants("buyer").First();
var parsedPlist = doc1.Descendants("row");
int i = 0;
str = new string[parsedPlist.Count()];
nam = new string[parsedPlist.Count()];
foreach (XElement elem in parsedPlist)
{
nam[i] = (string)elem.Element("btn_name");
str[i] = (string)elem.Element("string");
i++;
}
}
private void i1_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
if (nam[0] == buy_bac.Name)
{
Modules.msg = str[0];
if (Modules.msg != null)
{
if (popup.IsOpen != true)
{
popup.Child = tut;
popup.IsOpen = true;
popup.VerticalOffset = 200;
popup.HorizontalOffset = 50;
popup.IsOpen = true;
tut.lb_tut.Text = Modules.msg;
}
else
{
popup.IsOpen = false;
}
}
else if (nam[1] == buy_hme.Name)
{
Modules.msg = str[1];
if (Modules.msg != null)
{
if (popup.IsOpen != true)
{
popup.Child = tut;
popup.IsOpen = true;
popup.VerticalOffset = 400;
popup.HorizontalOffset = 50;
popup.IsOpen = true;
tut.lb_tut.Text = Modules.msg;
}
else
{
popup.IsOpen = false;
}
}
}
else if (nam[1] == buy_qestim.Name)
{
Modules.msg = str[2];
if (Modules.msg != null)
{
if (popup.IsOpen != true)
{
popup.Child = tut;
popup.IsOpen = true;
popup.VerticalOffset = 400;
popup.HorizontalOffset = 50;
popup.IsOpen = true;
tut.lb_tut.Text = Modules.msg;
}
else
{
popup.IsOpen = false;
}
}
}
}
}
private void i3_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
if (nam[2] == buy_piti.Name)
{
Modules.msg = str[3];
if (Modules.msg != null)
{
if (popup.IsOpen != true)
{
popup.Child = tut;
popup.IsOpen = true;
popup.VerticalOffset = 600;
popup.HorizontalOffset = 50;
popup.IsOpen = true;
tut.lb_tut.Text = Modules.msg;
}
else
{
popup.IsOpen = false;
}
}
}
else if (nam[2] == buy_rent.Name)
{
Modules.msg = str[4];
if (Modules.msg != null)
{
if (popup.IsOpen != true)
{
popup.Child = tut;
popup.IsOpen = true;
popup.VerticalOffset = 600;
popup.HorizontalOffset = 50;
popup.IsOpen = true;
tut.lb_tut.Text = Modules.msg;
}
else
{
popup.IsOpen = false;
}
}
}
else if (nam[2] == buy_qual.Name)
{
Modules.msg = str[5];
if (Modules.msg != null)
{
if (popup.IsOpen != true)
{
popup.Child = tut;
popup.IsOpen = true;
popup.VerticalOffset = 600;
popup.HorizontalOffset = 50;
popup.IsOpen = true;
tut.lb_tut.Text = Modules.msg;
}
else
{
popup.IsOpen = false;
}
}
}
else if (nam[2] == buy_mre.Name)
{
Modules.msg = str[6];
if (Modules.msg != null)
{
if (popup.IsOpen != true)
{
popup.Child = tut;
popup.IsOpen = true;
popup.VerticalOffset = 600;
popup.HorizontalOffset = 50;
popup.IsOpen = true;
tut.lb_tut.Text = Modules.msg;
}
else
{
popup.IsOpen = false;
}
}
}
}[/CODE]
The XML Parsed is
[QUOTE]<?xml version="1.0" encoding="utf-8" ?>
<plist version="1.0">
<Main_Screen>
<row>
<btn_name>Buyer Programs</btn_name>
<string>Answer your client’s difficult .</string>
</row>
<row>
<btn_name>Seller’s Programs</btn_name>
<string>What do sellers want s needs.</string>
</row>
</Main_Screen>
<abssas>
<row>
<btn_name>dat Programs</btn_name>
<string> your client’s </string>
</row>
<row>
<btn_name>my Programs</btn_name>
<string> sellers want s needs.</string>
</row>
</abssas>
</plist>[/QUOTE]
What is the problem with this?
Re: Cant Make instances with usercontrol
The xml file is included on your project? Is it Build Action as "Content"?
Re: Cant Make instances with usercontrol
yess.I tried it did not work.
Re: Cant Make instances with usercontrol
[QUOTE=yogeshg;901395]yess.I tried it did not work.[/QUOTE]
Can you provide a sample project? I dont seem to be able to replicate your problem.
Re: Cant Make instances with usercontrol
Hi yougeshg,
You shall not have any operation that may throw exception in UserControl constructor. This lines i think make troubles:
[CODE]
Popup popup = new Popup();
tutorial tut = new tutorial(); //another usercontrol
[/CODE]
If you like to create a child control tree , do it in onLoaded event not in the constructor. This construction works nice in Designer view for me. Also you can check [URL="http://msdn.microsoft.com/query/dev10.query?appId=Dev10IDEF1&l=EN-US&k=k%28VS.WPFDESIGNER.TROUBLESHOOTING%29;k%28TargetFrameworkMoniker-%22SILVERLIGHT%2cVERSION%3dV4.0%22%29&rd=true"]Troubleshooting WPF and Silverlight Designer Load Failures[/URL]
[CODE]
XAML:
<UserControl x:Class="jailbreaker.BoardControl"
Loaded="[B]OnLoad[/B]">
<controls:PivotItem Header="Start">
<Grid>
<my:BoardControl />
</Grid>
</controls:PivotItem>
CS:
public BoardControl()
{
InitializeComponent();
}
void [B]OnLoad[/B](object sender, RoutedEventArgs e)
{
try
{
accelerometer = new Accelerometer();
timer = new DispatcherTimer();
//...................
accelerometer.Start();
timer.Start();
}
catch (Exception ignore)
{
}
}
[/CODE]
do not forget to suppress possible exception in Designer surface with try/catch block , see above
Re: Cant Make instances with usercontrol
Hi,
you can use my article as a reference.
[URL="http://www.developer.nokia.com/Community/Wiki/Creating_custom_user_control_in_Expression_Blend_for_Windows_Phone"]http://www.developer.nokia.com/Community/Wiki/Creating_custom_user_control_in_Expression_Blend_for_Windows_Phone[/URL]
Examine the header of the MainPage.xaml to see if you have a link to the custom usercontrol you have created.
Re: Cant Make instances with usercontrol
Hi manikantan
The problem is not in creating and use UserControl . Sometimes WPF designer preview pane is not able to render your application page because your UserControl implementation does not follow design guide [URL="http://msdn.microsoft.com/query/dev10.query?appId=Dev10IDEF1&l=EN-US&k=k%28VS.WPFDESIGNER.TROUBLESHOOTING%29;k%28TargetFrameworkMoniker-%22SILVERLIGHT%2cVERSION%3dV4.0%22%29&rd=true#WritingCodeforDesignTime"]"Writing Code for Design Time"[/URL]. In that case WPF designer and application running on device or emulator behaves differently., your application is not broken but you cannot use the page preview
Regards,
Igor