小编Mar*_*age的帖子

使用WebClient.BaseAddress在Silverlight中获取基本URL的替代方法

在Silverlight应用程序中,我有时需要连接到托管应用程序的网站.为了避免在我的Silverlight应用程序中对网站进行硬编码,我使用如下代码:

WebClient webClient = new WebClient();
Uri baseUri = new Uri(webClient.BaseAddress);
UriBuilder uriBuilder = new UriBuilder(baseUri.Scheme, baseUri.Host, baseUri.Port);
// Continue building the URL ...
Run Code Online (Sandbox Code Playgroud)

WebClient为了访问XAP文件的URL,创建实例感觉非常笨拙.还有其他选择吗?

url silverlight webclient

2
推荐指数
2
解决办法
9550
查看次数

如何将美国的DateTime.Now转换为英国的当地时间?

希望你们所有人都在享受.

我的应用程序在美国的服务器上运行,它使用以下代码来选择日期和时间.

var pickUpTime = DateTime.Now.ToShortTimeString();
Run Code Online (Sandbox Code Playgroud)

我想要的是这个字符串应该花费英国时间而不是美国时间.

你能分享一下你的经历吗?

最好的问候,阿卜杜拉

.net asp.net

2
推荐指数
1
解决办法
2004
查看次数

C#覆盖ToString,以便在调试器中显示多行内容文本

ToString当我在debuger中鼠标悬停某个变量时,如何覆盖和格式化方法中的字符串以获取多行调试消息.目前,当我返回多行字符串(分隔\r\n)时,它仍然在调试器中以单行结束.

c# debugging tostring multiline

2
推荐指数
1
解决办法
1407
查看次数

我什么时候应该使用头文件?

在为当地一所大学的几名学生辅导之后,我被问到"与普通的.cpp文件相比,何时最好使用C++中的头文件?".我有点挣扎着答案,并且看看是否有一个更明确的答案,在哪些场景下最好的.

使用可在多个项目中使用的代码时最好使用头文件吗?我知道这是一个愚蠢的例子,但如果你做了自定义数学函数,可以在其他项目中反复使用; 那最好放在头文件中是否正确?

c++ oop

2
推荐指数
1
解决办法
288
查看次数

如何在 C# 中将文档类型添加到新的 XML 文件

如何将以下文档类型添加到 XML 文件的开头

<!DOCTYPE smil PUBLIC "-//W3C//DTD SMIL 2.0//EN" "http://www.w3.org/2001/SMIL20/SMIL20.dtd">
Run Code Online (Sandbox Code Playgroud)

我正在尝试使用 C# 创建 XML 文件XDocument,但无法弄清楚如何将上述内容添加到 xml 文件的开头

c# xml doctype

2
推荐指数
1
解决办法
7684
查看次数

使用c#,asp.net插入outlook约会

我希望在用户日历中插入约会,我可以使用Microsoft.Office.Interop.Outlook.Application(下面)轻松插入我自己,然后将用户添加为收件人,但如果我不想添加自己,如果我只想添加其他人?这可以用类似的方式完成吗?

谢谢

            Microsoft.Office.Interop.Outlook.Application app = null; 
            Microsoft.Office.Interop.Outlook.AppointmentItem appt = null; 
            app = new Microsoft.Office.Interop.Outlook.Application(); 

            appt = (Microsoft.Office.Interop.Outlook.AppointmentItem)app.CreateItem(Microsoft.Office.Interop.Outlook.OlItemType.olAppointmentItem); 
            appt.Subject = "Meeting "; 
            appt.Body = "Test"; 
            appt.Location = "a room"; 
            appt.Start = Convert.ToDateTime("08/08/2012 05:00:00 PM"); 
            appt.Recipients.Add("a@b.com"); 
            appt.End = Convert.ToDateTime("08/08/2012 6:00:00 PM"); 
            appt.ReminderSet = true; 
            appt.ReminderMinutesBeforeStart = 15; 
            appt.Importance = Microsoft.Office.Interop.Outlook.OlImportance.olImportanceHigh; 
            appt.BusyStatus = Microsoft.Office.Interop.Outlook.OlBusyStatus.olBusy; 
            appt.Save();
Run Code Online (Sandbox Code Playgroud)

.net c# asp.net outlook

2
推荐指数
1
解决办法
8441
查看次数

WP以编程方式移动ScrollViewer

我有一个ScrollViewer,我正在填充大约2个屏幕高度的控件.在底部,我有一个按钮,清除ScrollViewer并用不同的控件重新填充它.问题是当我重新填充它时,滚动距离保持在我的按钮所在的位置.我需要ScrollViewer滚动到顶部.我怎样才能做到这一点?

c# silverlight windows-phone

2
推荐指数
1
解决办法
1107
查看次数

如何编写LINQ查询以使用C#从另一个对象获取对象?

我将以下foreach循环转换为LINQ语句.

foreach (Plant plant in Plant.ListPlants)
{
    foreach (Program program in plant.GetAllPrograms())
    {
        if (program.GetProfitCenter() != null)
        {
            foreach (CostCenter costCenter in program.GetProfitCenter().GetAllCostCenters())
            {
                foreach (Account account in costCenter.GetAccounts())
                {
                    if (account.Code == Code)//Code is a parameter
                    {
                        return account;
                    }
                }
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

只要不存在此类帐户代码,结果应返回null.请帮我构建上述循环的LINQ.

.net c# linq loops object

2
推荐指数
1
解决办法
89
查看次数

异步方法中的异常处理未捕获异常的令人惊讶的情况

我的代码将异步方法与异常处理相结合。代码非常简单,等待所有任务,没有async void方法:

async Task DoWorkSafelyWithStateMachine()
{
    try
    {
        await DoWorkThatMightThrowException();
    }
    catch (Exception exception)
    {
        Console.WriteLine("With state machine: " + exception.Message);
    }
}
Run Code Online (Sandbox Code Playgroud)

等待这个方法不会抛出异常,因为异常被吞了:

await DoWorkSafelyWithStateMachine(); // No exception thrown
Run Code Online (Sandbox Code Playgroud)

但是,代码并不总是像预期的那样捕获异常。当该方法以稍微不同的方式编写时,编译器没有创建异步状态机,就会出现问题:

Task DoWorkSafelyWithoutStateMachine()
{
    try
    {
        return DoWorkThatMightThrowException();
    }
    catch (Exception exception)
    {
        Console.WriteLine("Without state machine: " + exception.Message);
        return Task.CompletedTask;
    }
}
Run Code Online (Sandbox Code Playgroud)

The method is not decorated with async and nothing is awaited inside the method. Instead the task returned by the method inside try is returned …

.net c# exception-handling try-catch async-await

2
推荐指数
1
解决办法
1538
查看次数

Threadpool正在被Windows服务问题使用

我创建了一个Windows服务,目前有三个计时器.第一个计时器每15秒唤醒一次,第二个计时器每分钟唤醒一次.第三个计时器每天都在醒来.

问题是这些是每次都会产生新的线程,而且一次线程池完全用完了.这就是产生3个线程并且不会产生更多的新线程.

我的代码看起来像这样:

protected Onstart()
{
  var timer1  = new TImer();
  timer.Elapsed += Event1;
  timer1.interval = 60000;
  timer1.start();

  var timer2  = new TImer();
  timer2.Elapsed += Event2;
  timer2.interval = 60000;
  timer2.start();
}

private Event1(object,elapsedeventargs)
{
  var workerthread1 = **new thread**(workerthreadfunc1)
  workerthread1.start();
}

private Event2(object,elapsedeventargs)
{
  var workerthread2 = **new thread**(workerthreadfunc2)
  workerthread2.start();
}
Run Code Online (Sandbox Code Playgroud)

因此,您可以看到它正在创建新线程,它将在某个时刻耗尽线程池中的所有线程并突然停止Windows服务.目前它正在停止并淹没活动ID为5000的evet日志.

c# windows multithreading threadpool

1
推荐指数
1
解决办法
1600
查看次数