小编Jou*_*usi的帖子

如何在单元测试中模拟 ApplicationRef

我试图通过 Jasmine 和 Angular 4 测试这两种方法,但this.applicationRef总是返回一个空对象。这个怎么解决?

这是我的代码:

@Injectable()
class Dialog {
  ....
  getRootViewContainerRef(): ViewContainerRef {
    const appInstance = this.applicationRef.components[0].instance;

    if (!appInstance.viewContainerRef) {
      const appName = this.applicationRef.componentTypes[0].name;
      throw new Error(`Missing 'viewContainerRef' declaration in ${appName} constructor`);
    }

    return appInstance.viewContainerRef;
  }
}

createOverlay(parentContainerRef: ViewContainerRef): ComponentRef<DialogContainerComponent> {
  const rootContainerRef = parentContainerRef;
  const rootInjector = rootContainerRef.injector;

  const bindings = ReflectiveInjector.resolve([]);
  const injector = ReflectiveInjector.fromResolvedProviders(bindings, rootInjector);

  const overlayFactory = this.cfr.resolveComponentFactory(DialogContainerComponent);
  return rootContainerRef.createComponent(overlayFactory, rootContainerRef.length, injector);
}
Run Code Online (Sandbox Code Playgroud)

这是我的测试脚本:

describe('Dialog service', () => {
  //let fixture: ComponentFixture<DialogInformationComponent>; …
Run Code Online (Sandbox Code Playgroud)

angular angular-test

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

Application.Current.MainPage 与 Navigation.PushAsync() 与 Navigation.PushModalAsync()

我正在开发不需要后退按钮的 Xamarin Forms App (PCL)。该应用程序有三个页面:一个SplashScreenPage用于加载数据,一个LoginPage是否需要用户登录,一个RootPageMasterDetailPage. 我想知道在页面之间导航的最佳选择是什么(例如避免内存泄漏):

第一个解决方案:

Application.Current.MainPage = new ContentPage();
Run Code Online (Sandbox Code Playgroud)

第二种解决方案:

Navigation.PushAsync(new NavigationPage(new ContentPage()));
Run Code Online (Sandbox Code Playgroud)

然后

NavigationPage.SetHasNavigationBar(this, false);
ClearNavigationStack();
Run Code Online (Sandbox Code Playgroud)

第三种解决方案

await Navigation.PushModalAsync(new NavigationPage(new ContentPage()));
Run Code Online (Sandbox Code Playgroud)

然后

NavigationPage.SetHasNavigationBar(this, false);
ClearModalStack();
Run Code Online (Sandbox Code Playgroud)

c# navigationbar xamarin xamarin.forms

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

'itoa':此项目的POSIX名称已弃用

我正在尝试通过执行以下操作将an int转换为string

int id = 12689;
char snum[MAX];
itoa(id, snum, 10);
Run Code Online (Sandbox Code Playgroud)

我收到以下错误:

'itoa':不建议使用此项目的POSIX名称。而是使用符合ISO C和C ++的名称:_itoa。

c itoa

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

浮点格式和isinf()

我目前正在处理C++中的浮点值.请考虑以下c ++代码段:

#include <cmath>
#include <cstring>
#include <iostream>

int main() {
    long double num;

    // Set num to a large, valid, floating point value
    memset(&num, 0xcc, sizeof(num));

    std::cout << "num = " << num << std::endl;
    std::cout << "isinf(num) = " << isinf(num) << std::endl;
    std::cout << "std::isinf(num) = " << std::isinf(num) << std::endl;

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

根据维基百科,这创建了一个80位扩展精度浮点值,因为我在x86机器上使用GCC.因此0xcccc cccc cccc cccc cccc,浮点值应该是有效值.

有趣的是,输出结果如下:

num = -4.77987e+986
isinf(num) = 1
std::isinf(num) = 0
Run Code Online (Sandbox Code Playgroud)

这让我想知道:

  • 为什么表现 …

c++ floating-point ieee-754 c++11

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

如何只阅读每行的第一个单词?

我做了很多简单的程序,但我只想char word[30]从文本文件的每一行读取第一个单词.

我试过了,但没有成功.哦,每次我阅读它都要重用那个char.(每次我阅读时都要列出一个有序列表).

任何人都可以通过一种简单而"清晰"的方式向我展示一种从文件中读取这种方式的方法吗?

FILE *fp;
char word[30];
fp = fopen("/myhome/Desktop/tp0_test.txt", "r");
if (fp == NULL) {
    printf("Erro ao abrir ficheiro!\n");
} else {
    while (!feof(fp)) {
        fscanf(fp,"%*[^\n]%s",word);//not working very well...
        printf("word read is: %s\n", word);
        strcpy(word,""); //is this correct?
    }
}
fclose(fp);
Run Code Online (Sandbox Code Playgroud)

例如,对于包含以下内容的文件:

word1 word5
word2 kkk
word3 1322
word4 synsfsdfs
Run Code Online (Sandbox Code Playgroud)

它只打印这个:

word read is: word2
word read is: word3
word read is: word4
word read is: 
Run Code Online (Sandbox Code Playgroud)

c file scanf

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

向窗口小部件添加标签

我正在尝试使用Qt向主窗口添加标签.这是一段代码:

int main(int argc, char *argv[]) {
    QApplication app(argc, argv);
    QWidget Main_Window;
    QPixmap Image;

    Image.load("1837.jpg");

    QLabel i_label;
    i_label.setPixmap(Image);
    i_label.show();

    QPushButton Bu_Quit("Quit", &Main_Window);

    QObject::connect(&Bu_Quit, SIGNAL(clicked()), qApp, SLOT(quit()));

    Main_Window.show();
    return app.exec();
}
Run Code Online (Sandbox Code Playgroud)

我一直很难弄清楚如何正确地添加QLabels到QWidgets,我试图Main_Window使用这种方法将其设置为主要小部件:app.setMainWidget(Main_Window)并且标签仍然在窗口之外.那么如何使用Qt将标签放入小部件?

c++ qt

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

了解NotUsed和Done

我有一个很难理解的目的和意义NotUsed,并Done在阿卡流.

让我们看看以下2个简单的例子:

使用NotUsed:

implicit val system = ActorSystem("akka-streams")
implicit val materializer = ActorMaterializer()

val myStream: RunnableGraph[NotUsed] =
  Source.single("stackoverflow")
  .map(s => s.toUpperCase())
  .to(Sink.foreach(println))

val runResult:NotUsed = myStream.run()
Run Code Online (Sandbox Code Playgroud)

使用完成

implicit val system = ActorSystem("akka-streams")
implicit val materializer = ActorMaterializer()

val myStream: RunnableGraph[Future[Done]] =
  Source.single("stackoverflow")
  .map(s => s.toUpperCase())
  .toMat(Sink.foreach(println))(Keep.right)

val runResult: Future[Done] = myStream.run()
Run Code Online (Sandbox Code Playgroud)

当我运行这些示例时,在两种情况下都得到相同的输出:

STACKOVERFLOW //output
Run Code Online (Sandbox Code Playgroud)

那么NotUsed和Done到底是什么?有什么区别,什么时候我应该优先于另一个?

scala akka akka-stream

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

注意:C++不支持default-int

我在Visual Studio中收到此消息:

注意:C++不支持default-int

我的C代码出了什么问题?

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

void remplire (int t[], int n);
void afficher (int t[], int n);

void main ()
{
    const long_tab = 2000;
    int t[long_tab];
    srand (time(NULL));
    remplire (t, long_tab);
    afficher (t, long_tab);
}

void remplire (int t[], int n)
{
    int i;
    for (i = 0; i <= n; i++)
    {
        t[i] = rand (); 
    }
}

void afficher (int t[], int n)
{
    int i;
    for (i = 0; i <= …
Run Code Online (Sandbox Code Playgroud)

c visual-studio-2008

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

在web.config中存储连接字符串

为什么我们在web.config文件中存储连接字符串?这样做有什么好处?

asp.net connection-string web-config

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

从请求URL中检索值

我有一个URL,它将在Java中调用方法my方法.它看起来像这样:

http://www.example.com/method?value=24
Run Code Online (Sandbox Code Playgroud)

如何检索值24并在我的被调用方法中使用它?

java url wicket

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