this doesnt work ,i have an item "Area" in my listBox1,but code is not producing MessageBox in wp7 runtime,
private void button1_Click(object sender, RoutedEventArgs e)
{
if( listBox1.SelectedItem=="Area")
{
MessageBox.Show("hi");
}
}
try:
listBox1.SelectedItem as string == "Area"
You are missing the cast.
Related
I am developing Umbraco 7 MVC application and my requirement is to add Item inside Umbraco. Item name should be unique. For that used the below code but I am getting the error "Oops: this document is published but is not in the cache (internal error)"
protected override void ApplicationStarting(UmbracoApplicationBase umbracoApplication,
ApplicationContext applicationContext)
{
ContentService.Publishing += ContentService_Publishing;
}
private void ContentService_Publishing(IPublishingStrategy sender, PublishEventArgs<IContent> e)
{
try
{
if(newsItemExists)
{
e.Cancel = true;
}
}
catch (Exception ex)
{
e.Cancel = true;
Logger.Error(ex.ToString());
}
}
Then I tried adding code to unpublish but its not working i.e the node is getting published. Below is my code
private void ContentService_Publishing(IPublishingStrategy sender, PublishEventArgs<IContent> e)
{
try
{
int itemId=1234; //CurrentPublishedNodeId
if(newsItemExists)
{
IContent content = ContentService.GetById(itemId);
ContentService.UnPublish(content);
library.UpdateDocumentCache(item.Id);
}
}
catch (Exception ex)
{
e.Cancel = true;
Logger.Error(ex.ToString());
}
}
But with the above code, if you give the CurrentPublishedNodeId=2345 //someOthernodeId its unpublished correctly.
Can you please help me on this issue.
You don't have to do this, Umbraco will automatically append (1) to the name if the item already exists (so it IS unique).
If you don't want this behavior you can check in the following way:
protected override void ApplicationStarting(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
{
ContentService.Publishing += ContentService_Publishing;
}
private void ContentService_Publishing(Umbraco.Core.Publishing.IPublishingStrategy sender, PublishEventArgs<IContent> e)
{
var contentService = UmbracoContext.Current.Application.Services.ContentService;
// It's posible to batch publish items, so go through all items
// even though there might only be one in the list of PublishedEntities
foreach (var item in e.PublishedEntities)
{
var currentPage = contentService.GetById(item.Id);
// Go to the current page's parent and loop through all of it's children
// That way you can determine if any page that is on the same level as the
// page you're trying to publish has the same name
foreach (var contentItem in currentPage.Parent().Children())
{
if (string.Equals(contentItem.Name.Trim(), currentPage.Name.Trim(), StringComparison.InvariantCultureIgnoreCase))
e.Cancel = true;
}
}
}
I think your problem might be that you're not looping through all PublishedEntities but using some other way to determine the current page Id.
Note: Please please please do not use the library.UpdateDocumentCache this, there's absolutely no need, ContentService.UnPublish will take care of the cache state.
I am working with an attached behavior for logging user actions on a ScrollBar.
my code:
class ScrollBarLogBehavior : Behavior<ScrollBar>
{
protected override void OnAttached()
{
base.OnAttached();
AssociatedObject.Loaded += new RoutedEventHandler(AssociatedObject_Loaded);
}
void AssociatedObject_Loaded(object sender, RoutedEventArgs e)
{
...
var track = (Track)AssociatedObject.Template.FindName("PART_Track", AssociatedObject);
// ** HERE is the problem: track is null ! **
...
}
How can I detect that the template has loaded and I can find the Track?
(when I call AssociatedObject.Template.LoadContent() the result containt the requested Track, so it i a matter of timing and not a matter of wrong template or naming)
Override the method OnApplyTemplate
public override void OnApplyTemplate()
{
base.OnApplyTemplate();
var textBox = Template.FindName("PART_Textbox", this) as TextBox;
}
I did not find any good way to detect when the template was loaded. However, I did find a way to find the Track:
in OnAttached() - register to Scroll event fo the ScrollBar (this can only happen after the entire template is loaded, of course):
protected override void OnAttached()
{
base.OnAttached();
_scrollHandler = new ScrollEventHandler(AssociatedObject_Scroll);
AssociatedObject.AddHandler(ScrollBar.ScrollEvent, _scrollHandler, true);
}
When handling the Scroll event, remove registration and find the Thumb:
void AssociatedObject_Scroll(object sender, ScrollEventArgs e)
{
var track = (Track)AssociatedObject.Template.FindName("PART_Track", Associated
if (track == null)
return;
AssociatedObject.RemoveHandler(ScrollBar.ScrollEvent, _scrollHandler);
// do my work with Track
...
}
If I understand correctly, you wish to create an attached behavior that will reference a template part after the ScrollBar has been loaded.
The following should work:
internal static class ScrollBarLogBehavior
{
public static readonly DependencyProperty LogUserActionProperty = DependencyProperty.RegisterAttached(
"LogUserAction",
typeof(bool),
typeof(ScrollBarLogBehavior),
new UIPropertyMetadata(default(bool), LogUserActionChanged));
public static bool GetLogUserAction(DependencyObject obj)
{
return (bool)obj.GetValue(LogUserActionProperty);
}
public static void SetLogUserAction(DependencyObject obj, bool value)
{
obj.SetValue(LogUserActionProperty, value);
}
public static void LogUserActionChanged(DependencyObject s, DependencyPropertyChangedEventArgs e)
{
if (s is ScrollBar scrollBar)
{
scrollBar.Loaded += OnScrollBarLoaded;
}
}
private static void OnScrollBarLoaded(object sender, RoutedEventArgs e)
{
if (sender is ScrollBar scrollBar)
{
if (scrollBar.Template != null)
{
// I'm not sure, but the `name` in the following method call might be case sensitive.
if (scrollBar.Template.FindName("PART_Track", scrollBar) is Track track)
{
// do work with `track` here
}
}
}
}
}
where you would "attach" the behavior in your XAML with:
<ScrollBar guiControls:ScrollBarLogBehavior.LogUserAction="True">
<!-- more here -->
</ScrollBar>
BE ADVISED: this implementation completely ignores the bool value that is being set for LogUserAction
I am trying to browse web pag1es using a listbox, I have added three links to it. All three links are loading well but when third link finishes loading I got this exception.
Exception is:
InvelidArguement = value of '3' is not valid for 'SelectedIndex'. Parameter name:
SelectedIndex
Warning is:
`The result of the exception is always 'true' since a value of type 'int' is
never equal to 'null' of type 'int?'
this is my program image:
this is my program code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
listBox1.Items.Add("www.google.com");
listBox1.Items.Add("www.facebook.com");
listBox1.Items.Add("www.yahoo.com");
listBox1.SelectedIndex = 0;
listBox1.DataSource = listBox1.Items;
}
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
do
{
webBrowser1.Navigate(listBox1.SelectedItem.ToString());
while (webBrowser1.ReadyState != WebBrowserReadyState.Complete)
{
Application.DoEvents();
if(webBrowser1.ReadyState == WebBrowserReadyState.Complete)
{
listBox1.SelectedIndex = listBox1.SelectedIndex+1;
}
}
} while (listBox1.SelectedIndex != null);
}
private void Form1_Load(object sender, EventArgs e)
{
}
}
}
This statement here is causing an issue. When the selected index (in your example 0 - 2) reaches 3, it will throw an exception because there isn't an index of 3 available.
listBox1.SelectedIndex = listBox1.SelectedIndex+1;
Your loop will also never end because the SelectedIndex returns an integer which can never return null. You'll want to modify your code to check for length instead using an new index integer. Keep in mind the count will always return one value higher than the index (count starts at 1, index starts at 0).
I am trying to make a redirect from my not www domain to my www domain like this:
protected void Application_BeginRequest(object sender, EventArgs e)
{
if (HttpContext.Current.Request.Url.ToString().Contains("http://mydomain.com"))
{
HttpContext.Current.Response.Status = "301 Moved Permanently";
HttpContext.Current.Response.AddHeader("Location", "http://www.mydomain.com");
}
}
For some reason the redirect is not done...(ok, I know this can be done by defining a CNAME also but I am simply wondering why this is not working...
I think it's something small buggy here...but I am not able to see it.
Use this tool to verify. May be you have to change code little as below,
protected void Application_BeginRequest(object sender, EventArgs e)
{
if (HttpContext.Current.Request.Url.ToString().Contains("http://mydomain.com"))
{
HttpContext.Current.Response.Status = "301 Moved Permanently";
HttpContext.Current.Response.AddHeader("Location",
Request.Url.ToString().ToLower().Replace(
"http://mydomain.com",
"http://www.mydomain.com"));
}
}
i have weird exception by SaveFileDialog in Silverlight 3. I don't really have a idea where the problem is.
I create instance of SaveFileDialog in Loaded event of user control. After Download button is clicked and dialogResult is true asynchronous file download is started. After file download is completed, method OpenFile() is called. This works fine once, but second time I get exception:
Exception message:
"No file was selected"
Details:
{System.InvalidOperationException: No file was selected.
at System.Windows.Controls.SaveFileDialog.OpenFile()
at Spaces.Client.Views.Dialogs.FileDialog.BL_DownloadFileCompleted(Object sender, EventArguments`1 e)
at Spaces.Client.BL.Interface.DownloadFileCompletedEventHandler.Invoke(Object sender, EventArguments`1 e)
at Spaces.Client.BL.WebService.SpacesService._spacesService_DownloadFileCompleted(Object sender, DownloadFileCompletedEventArgs e)
at System.EventHandler`1.Invoke(Object sender, TEventArgs e)
at Spaces.Client.BL.SpacesServiceReference.ServiceClient.OnDownloadFileCompleted(Object state)}
Stack:
at System.Windows.Controls.SaveFileDialog.OpenFile()
at Spaces.Client.Views.Dialogs.FileDialog.BL_DownloadFileCompleted(Object sender, EventArguments`1 e)
at Spaces.Client.BL.Interface.DownloadFileCompletedEventHandler.Invoke(Object sender, EventArguments`1 e)
at Spaces.Client.BL.WebService.SpacesService._spacesService_DownloadFileCompleted(Object sender, DownloadFileCompletedEventArgs e)
at System.EventHandler`1.Invoke(Object sender, TEventArgs e)
at Spaces.Client.BL.SpacesServiceReference.ServiceClient.OnDownloadFileCompleted(Object state)
Here code snippet:
private void _userControlFileDialog_Loaded(object sender, RoutedEventArgs e)
{
_comboBoxVersions.ItemsSource = _file.Versions;
if (_comboBoxVersions.Items.Count > 0)
_comboBoxVersions.SelectedIndex = 0;
String extension = "*." + _file.Extension;
_sfd = new SaveFileDialog();
_sfd.DefaultExt = _file.Extension;
_sfd.Filter = extension + "|" + extension;
}
private void _hyperlinkButtonDownload_Click(object sender, RoutedEventArgs e)
{
string path = ((FileVersion)_comboBoxVersions.SelectedItem).Url;
bool? dialogResult = _sfd.ShowDialog();
if (dialogResult == true)
{
AppContext.BL.DownloadFileCompleted += new Spaces.Client.BL.Interface.DownloadFileCompletedEventHandler(BL_DownloadFileCompleted);
AppContext.BL.DownloadFileAsync(AppContext.AuthenticatedUser, path);
}
}
void BL_DownloadFileCompleted(object sender, Spaces.Client.BL.Interface.EventArguments<byte[]> e)
{
byte [] data = e._result;
using (Stream fileStream = (Stream)_sfd.OpenFile())
{
fileStream.Write(data, 0, data.Length);
fileStream.Flush();
fileStream.Close();
}
}
Have anybody idea what is wrong?
Regards
Anton Kalcik
There was problem with multiple event handlers. On each click is event handler attached and never detached. Event handler stays attached also after UserControl is closed. So it is on developer to detach event handler on properly way.
Regards
AKa